mongodb - how to return 0 for count of match that doesn't exist -
so i'm using aggregation match if value in array group values:
db.person.aggregate([ { $match: {"city":{ $in: ["new york", "denver"]}} },{ $group: {_id: "$city", total : {$sum:1}} } ]) {_id:"new york", total:5} {_id:"denver", total:2} this works in case city in values return total:0 in case city isn't in set. ie:
["new york", "denver", "orlando"] {_id:"new york", total:5} {_id:"denver", total:2} {_id:"orlando", total:0} is possible or should check results in code , append 0 value results results?
Comments
Post a Comment