I have a simple collection with documents of vendors that looks like this:
{
"name": "Nick's Burger",
"country": "AT",
"branches": [
{
"branchId": "12535",
"branchName": "Nick's Burger Branch One",
"branchAddress": "Somewhere in austria"
},
{
"branchId": "38267",
"branchName": "Nick's Burger Branch Two",
"branchAddress": "Somewhere else in austria"
}
]
}
For each of the vendors there is a list of "branches", and I want to count how many branches all the vendors from "country" AT have.
tried something like:
db.vendors.find({country: "AT"}, {$size: {branches: 1}}).count()
but that only gives me the count of vendors from AT...
db.vendors.aggregate([ { '$match': { 'country': "AT" } }, { '$project': { 'branchesCount': { '$size': '$branches' } } } ])db.vendors.aggregate[{"$match":{"country":"AT"}},{"$group":{"_id":null,"branchesCount":{"$sum":{"$size":"$branches"}}}}])