Q:

Mongodb split result in arrays based on property

The data looks like this - in three different documents :
{ "location":  "X", // LOCATION X
   "property1": "value",
   "property2": "value"} 

 { "location":  "X", // LOCATION X
   "property1": "value",
   "property2": "value"}

 { "location":  "Y", // LOCATION: Y
   "property1": "value",
   "property2": "value"}

The entries should be sorted by location and all entries which have the same location should all be in the same array.
So in the example the mongodb query should return an array which contains only data where the location is equal to "X" and another array with only one entry where the location is equal to "Y".
I don't want to do that work in the frontend. How would I do that in mongodb using Node.js?

A:

You can use MongoDB aggregation framework for that
https://docs.mongodb.com/manual/reference/operator/aggregation/group/
db.myCol.aggregate(
   [
      {
        $group : {
           _id : "$location", entries:{$push: {property1:"$property1", property2:"$property2"}}
    }
       }
])

If you want the whole object, replace
{property1:"$property1", property2:"$property2"}
with 
"$$ROOT"
db.myCol.aggregate(
   [
      {
        $group : {
           _id : "$location", entries:{$push: "$$ROOT"}
    }
       }
])

