MongoDB聚合 - 如何在单个查询中执行多个操作?

时间:2018-01-27 07:04:27

标签: mongodb aggregation-framework

我正在设计插座应用程序的后端,我需要根据插座所有者上传的图像编写查询以显示单个插座图像。我还需要计算用户上传的图像和插座所有者上传的图像。那么,我如何使用mongoDB聚合查询实现这一目标。以下是我的询问:

db.images.aggregate([
      {
        $match: {
          'storeId': storeId,
          'imageType': 1
        }
      },
      {
        $project: {
          '_id': 0,
          'image': 1,
          'imageId': 1,
          'caption': 1,
          'likes': 1,
          'dislikes': 1,
          'uploadTime': 1,
          'imageType': 1
        }
      },
      {
        $match: {
          'storeId': storeId,
          $or: [{ imageType: 1 }, { imageType: 3 }]
        }
      },
      {
        $count: "totalCount"
      } 
    ]

以下是数据库中示例文档的快照:

[
  {
    "imageId": "siyaram_image_1",
    "reviewId": "#review_id",
    "storeId": "#store_id",
    "username": "abc@xyz.com",
    "image": "reviews/siyaram_image_1.jpg",
    "imageType": 1,
    "likes": 0,
    "dislikes": 0,
    "status": 4
  },
  {
    "imageId": "siyaram_image_2",
    "reviewId": "#review_id",
    "storeId": "#store_id",
    "username": "abc@xyz.com",
    "image": "reviews/siyaram_image_2.jpg",
    "imageType": 1,
    "likes": 0,
    "dislikes": 0,
    "status": 4
  },
  {
    "imageId": "siyaram_image_3",
    "reviewId": "#review_id",
    "storeId": "#store_id",
    "username": "abc@xyz.com",
    "image": "reviews/siyaram_image_3.jpg",
    "imageType": 3,
    "likes": 0,
    "dislikes": 0,
    "status": 4
  },
  {
    "imageId": "siyaram_image_4",
    "reviewId": "#review_id",
    "storeId": "#store_id",
    "username": "abc@xyz.com",
    "image": "reviews/siyaram_image_4.jpg",
    "imageType": 3,
    "likes": 0,
    "dislikes": 0,
    "status": 4
  },
  {
    "imageId": "siyaram_image_5",
    "reviewId": "#review_id",
    "storeId": "#store_id",
    "username": "abc@xyz.com",
    "image": "reviews/siyaram_image_5.jpg",
    "imageType": 3,
    "likes": 0,
    "dislikes": 0,
    "status": 4
  }
]

预期结果:

{
  "total_image_count": 5,
  "images": [
    {
      "imageId": "siyaram_image_1",
      "reviewId": "#review_id",
      "storeId": "#store_id",
      "username": "abc@xyz.com",
      "image": "reviews/siyaram_image_1.jpg",
      "imageType": 1,
      "likes": 0,
      "dislikes": 0,
      "status": 4
    },
    {
      "imageId": "siyaram_image_2",
      "reviewId": "#review_id",
      "storeId": "#store_id",
      "username": "abc@xyz.com",
      "image": "reviews/siyaram_image_2.jpg",
      "imageType": 1,
      "likes": 0,
      "dislikes": 0,
      "status": 4
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

您可以使用$facet$facet可以对不同字段的同一文档执行多项操作

db.images.aggregate(
    [
        {$facet : {
            totalCount : [
                { $match : {"storeId" : "#store_id"} },
                { $count : "storeId" }
            ],
            images : [
                { $match : {"storeId" : "#store_id", "imageType": 1} }
            ]
        }},
        {$addFields : {totalCount : {$arrayElemAt : ["$totalCount.storeId",0]}}}
    ]
)

输出

> db.images.aggregate( [ {$facet : { totalCount : [ { $match : {"storeId" : "#store_id"} }, { $count : "storeId" } ], images : [ { $match : {"storeId" : "#store_id", "imageType": 1} } ] }}, {$addFields : {totalCount : {$arrayElemAt : ["$totalCount.storeId",0]}}} ] ).pretty()
{
    "totalCount" : 5,
    "images" : [
        {
            "_id" : ObjectId("5a6c2ac0a52367a6bc65755e"),
            "imageId" : "siyaram_image_1",
            "reviewId" : "#review_id",
            "storeId" : "#store_id",
            "username" : "abc@xyz.com",
            "image" : "reviews/siyaram_image_1.jpg",
            "imageType" : 1,
            "likes" : 0,
            "dislikes" : 0,
            "status" : 4
        },
        {
            "_id" : ObjectId("5a6c2ac0a52367a6bc65755f"),
            "imageId" : "siyaram_image_2",
            "reviewId" : "#review_id",
            "storeId" : "#store_id",
            "username" : "abc@xyz.com",
            "image" : "reviews/siyaram_image_2.jpg",
            "imageType" : 1,
            "likes" : 0,
            "dislikes" : 0,
            "status" : 4
        }
    ]
}
>