MongoDB group with multiple id
For MongoDB's $group-operator, no value is also a value.
When you want to exclude any documents where not all three keys are present, you can add a $match-step to your aggregation pipeline which filters any documents which do not have all these keys.
db.collection.aggregate([
{ $match: {
"type" : { "$exists" : true},
"location" : { "$exists" : true},
"language" : { "$exists" : true}
}
},
{ $group: {
"_id": {
"location": "$location",
"type": "$typ",
"language": "$language"
},
"count": {$sum: 1}
}
}
]);
Query:
db.collection.aggregate([{
$match: {
type: {
"$exists": true
},
location: {
"$exists": true
},
language: {
"$exists": true
}
}
}])