find element with array count mongodb code example
Example 1: mongo count elements in array
> db.mycollection.insert({'foo':[1,2,3,4]})
> db.mycollection.insert({'foo':[5,6,7]})
> db.mycollection.aggregate([{$project: { count: { $size:"$foo" }}}])
{ "_id" : ObjectId("5314b5c360477752b449eedf"), "count" : 4 }
{ "_id" : ObjectId("5314b5c860477752b449eee0"), "count" : 3 }
Example 2: Mongodb count based on array of ids
db.collection.aggregate([
{ $unwind: "$connects" },
{ "$group": { "_id": {skill: "$skill", parser_id: "$connects.parser_id"}, "count": { "$sum": 1 } }},
{ "$group": { "_id": "$_id.skill", "quantity": { "$sum": 1 } }},
{ $project: { 'skill': '$_id', 'quantity': 1, _id: 0 } }
])