Simple collection:
[
{
_id: '123',
name: 'FooBar',
zone: 'Bas'
},{
_id: '456',
name: 'Alice',
zone: 'Bas'
},{
_id: '789',
name: 'FooBar',
zone: 'Bas'
}
]
First I build a query to find all elements by name:
db.collection.find({name:'FooBar'})
How can I extend this with a group query using aggregation? I would like to group the collection by zone.
Try this,
db.collection.aggregate(
{
$match : {name : "FooBar"}
},
{
$group : {
_id : "$zone",
total : { $sum : 1 }
}
}
);
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