0

Right now i use this command :

restos.aggregate([{$unwind:"$categories"},
{$group:{_id:'$categories.title', count:{$sum:1}}}, 
{$project:{ type:"$_id", count: 1,_id:0}}])

which outputs me data like this :

{ "count" : 3, "type" : "Sandwiches" }

What i'd like is to have this instead:

{ "Sandwiches" : 3 }

How can i achieve this ? I am using python with pymongo, will i need to format it manually ?

Thanks

1 Answer 1

2

You can add an stage into aggregation with $replaceRoot and $arrayToObject like this:

{
  "$replaceRoot": {
    "newRoot": {
      "$arrayToObject": [
        [
          {
            "k": "$type",
            "v": "$count"
          }
        ]
      ]
    }
  }
}

Example here

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.