mongo count elements in array mongo count elements in array Mongodb count based on array of ids

The solution for “mongo count elements in array mongo count elements in array Mongodb count based on array of ids” can be found here. The following code will assist you in solving the problem.

> 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 }
{ $size: }
db.collection.aggregate([
{ $unwind: “$connects” },

// count all occurrences
{ “$group”: { “_id”: {skill: “$skill”, parser_id: “$connects.parser_id”}, “count”: { “$sum”: 1 } }},

// sum all occurrences and count distinct
{ “$group”: { “_id”: “$_id.skill”, “quantity”: { “$sum”: 1 } }},

// (optional) rename the ‘_id’ attribute to ‘skill’
{ $project: { ‘skill’: ‘$_id’, ‘quantity’: 1, _id: 0 } }
])

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in