Count values in array of objects

I have a simpler answer for you:

db.test01.aggregate([
… { $unwind: “$votes” },
… { $sortByCount: “$votes.action” }
… ])
{ “_id” : “up”, “count” : 2 }
{ “_id” : “down”, “count” : 1 }

All the best,

– Rodrigo

3 Likes