Je kunt dit enigszins belachelijk doen met $facet
en $ifNull
aggregatie
db.collection.aggregate([
{ "$facet": {
"array": [
{ "$match": { "type": "L", "lampStatus": "ON" }},
{ "$group": {
"_id": null,
"TotalLights": { "$sum": 1 }
}},
{ "$project": { "_id": 0, "TotalLights": 1 }}
]
}},
{ "$project": {
"TotalLights": {
"$ifNull": [{ "$arrayElemAt": ["$array.TotalLights", 0] }, 0 ]
}
}}
])