如何在mongodb聚合中对多个文档进行分组

Vik*_*hha 4 mongodb aggregation-framework

我有一个名为 Offers 的集合,下面是示例文档,

{
  "offerId": "3a06d230-5836-44c2-896b-f5bfb6b27a77",
  "outlets": {
    "storeUuid": "b3da5136-15a4-4593-aabd-4788f7d80f19",
    "location": {
      "type": "Point",
      "coordinates": [
        77,
        22
      ]
    }
  }"startTime": "2018-04-06T08:03:37.954Z",
  "endTime": "2018-04-07T07:35:00.046Z"
},
{
  "offerId": "3a06d230-5836-44c2-896b-f5bfb6b27a77",
  "outlets": {
    "storeUuid": "f18a9a9e-539e-4a9e-b313-d947e2ce76de",
    "location": {
      "type": "Point",
      "coordinates": [
        77,
        22
      ]
    }
  },
  "startTime": "2018-04-06T08:03:37.954Z",
  "endTime": "2018-04-07T07:35:00.046Z"
},
{
  "offerId": "e6c1f140-6407-4481-9a18-56789d90f549",
  "outlets": {
    "storeUuid": "b3cdd08d-f7f5-4544-8279-08489974148c",
    "location": {
      "type": "Point",
      "coordinates": [
        77,
        22
      ]
    }
  },
  "startTime": "2018-04-05T12:30:37.954Z",
  "endTime": "2018-04-08T12:38:00.046Z"
},
{
  "offerId": "e6c1f140-6407-4481-9a18-56789d90f549",
  "outlets": {
    "storeUuid": "09d6fc18-9d5c-4b4f-8de1-c6f555b8a370",
    "location": {
      "type": "Point",
      "coordinates": [
        77,
        22
      ]
    }
  },
  "startTime": "2018-04-05T12:30:37.954Z",
  "endTime": "2018-04-08T12:38:00.046Z"
},
{
  "offerId": "e6c1f140-6407-4481-9a18-56789d90f549",
  "outlets": {
    "storeUuid": "bf71e102-9da1-47b5-81e1-98d27f20bcf4",
    "location": {
      "type": "Point",
      "coordinates": [
        77,
        22
      ]
    }
  },
  "startTime": "2018-04-05T12:30:37.954Z",
  "endTime": "2018-04-08T12:38:00.046Z"
}
Run Code Online (Sandbox Code Playgroud)

我想分组offerId,结果应该是

[
  {
    "offerId": "e6c1f140-6407-4481-9a18-56789d90f549",
    "outlet": [
      {
        "storeUuid": "bf71e102-9da1-47b5-81e1-98d27f20bcf4",
        "location": {
          "type": "Point",
          "coordinates": [
            77,
            22
          ]
        }
      },
      {
        "storeUuid": "09d6fc18-9d5c-4b4f-8de1-c6f555b8a370",
        "location": {
          "type": "Point",
          "coordinates": [
            77,
            22
          ]
        }
      },
      {
        "storeUuid": "b3cdd08d-f7f5-4544-8279-08489974148c",
        "location": {
          "type": "Point",
          "coordinates": [
            77,
            22
          ]
        }
      }
    ],
    "startTime": "2018-04-05T12:30:37.954Z",
    "endTime": "2018-04-08T12:38:00.046Z"
  },
  {
    "offerId": "3a06d230-5836-44c2-896b-f5bfb6b27a77",
    "outlet": [
      {
        "storeUuid": "f18a9a9e-539e-4a9e-b313-d947e2ce76de",
        "location": {
          "type": "Point",
          "coordinates": [
            77,
            22
          ]
        }
      },
      {
        "storeUuid": "b3da5136-15a4-4593-aabd-4788f7d80f19",
        "location": {
          "type": "Point",
          "coordinates": [
            77,
            22
          ]
        }
      }
    ],
    "startTime": "2018-04-06T08:03:37.954Z",
    "endTime": "2018-04-07T07:35:00.046Z"
  }
]
Run Code Online (Sandbox Code Playgroud)

到目前为止我的聚合查询,

db.offers.aggregate([
  {
    $group: {
      _id: "$offerId",
      outlet: {
        $addToSet: "$outlets"
      }
    }
  }
])
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激

jon*_*hid 7

像这样做

添加所需字段的投影。

按您想要的字段分组

创建一个新属性并将嵌套字段推送到它

db.getCollection('offers').aggregate([

   { $project : { offerId : 1 , outlets : 1, startTime: 1, endTime: 1  } },
   { $group: { 
       _id: "$offerId" , 
        outlet: { 
            $push: { 
                 storeUuid : "$outlets.storeUuid", 
                 location: "$outlets.location"
            } 
        },
        startTime: { "$first": "$startTime"},
        endTime: { "$first": "$endTime"}
      } 
    }

])
Run Code Online (Sandbox Code Playgroud)