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
CodePudding user response:
You can add an stage into aggregation with $replaceRoot and $arrayToObject like this:
{
"$replaceRoot": {
"newRoot": {
"$arrayToObject": [
[
{
"k": "$type",
"v": "$count"
}
]
]
}
}
}
Example here