1

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?

1 Answer 1

3

You should use the dot notation for your _id

db.getCollection("users").aggregate([
   {
      $group: { _id:  "$status.statusId", count: {$sum:1} }
   }
])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.