danfran
9/21/2015 - 8:49 AM

Mongodb Aggregate with Array of values

Mongodb Aggregate with Array of values

In case you want to group some values in an array you can add '$push' inside '$group' to create an array with these values:

$> db.someColl.aggregate([{ $match: {field_one: 'value_here' } },  { $group: { _id: "$field_two", count:{$sum: 1}, our_data: { $push: "$field_three" } } }, { $sort: {count: -1} } ], { allowDiskUse: true } )

You can also extend $push with properties, so instead of:

$push: "$field_three"

you can have:

$push: { my_prop: "$field_three", ... }