Reputation: 1009
I have a collection of documents of format
{
"name": <name>,
"status": {
"statusId": <can be a number which I want to aggregate like 1/2/3/4>
}
}
I want to have an aggregation by status.statusId
- e.g. 5 documents have status.statusId
= 0, 3 documents have status.statusId
= 5 etc.
I tried
db.getCollection("users").aggregate([
{
$group: { _id: { "status": { "statusId": "$statusId" }}, count: {$sum:1} }
}
])
but it doesn't work.
Where am I going wrong?
Upvotes: 1
Views: 1589
Reputation: 49985
You should use the dot notation for your _id
db.getCollection("users").aggregate([
{
$group: { _id: "$status.statusId", count: {$sum:1} }
}
])
Upvotes: 3