I have a collection named offers and a sample documents are below,
{
  "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"
}
I want to group by offerId and result should be
[
  {
    "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"
  }
]
My aggregation query so far,
db.offers.aggregate([
  {
    $group: {
      _id: "$offerId",
      outlet: {
        $addToSet: "$outlets"
      }
    }
  }
])
Any help would be appreciated
Do it like this
Add a projection of the fields that you want.
Group by your desired field
Create a new propertie and push to it the nested fields
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"}
      } 
    }
])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With