aggregation framework - using MongoDB aggregate count subdata -
here data in mongodb:
{ "data": { "order_goods": [{ "category": 235 }, { "category": 666 }] } }, { "data": { order_goods: [{ "category": 235 }] } }
here expected output:
{"category":235, "total":2} {"category":666, "total":1}
i have try many ways aggregate such $group
, result like:
{ "_id" : [ 235, 666 ], "total" : 1 } { "_id" : [ 235 ], "total" : 1 }
thanks help.
please try below query :
db.collection.aggregate( [ { "$unwind": "$data.order_goods" }, { "$group" : { "_id" : "$data.order_goods.category", "total": {"$sum":1} } }, { "$project" : { "category" : "$_id" , total : 1 } ] );
Comments
Post a Comment