【发布时间】:2019-02-05 02:47:53
【问题描述】:
我是 Mongo Db 的新手,希望对这个查询有所帮助。我写了 mongodb 聚合一个我的字段 "lampStatus": 'OFF' is 30 records are there and "lampStatus": 'ON' is no records are there我出来了 {"OFF" : 30} 我没有得到 {"ON" : 0} 值请任何人建议我。
db.collection.aggregate([
{ $match:{"type" : "L"}},
{ "$facet": {
"ON": [
{ "$match" : {"lampStatus":'ON'}},
{ "$count": "ON" }
],
"OFF": [
{ "$match" : {"lampStatus": 'OFF'}},
{ "$count": "OFF" }
]
}},
{ "$project": {
"ON": { "$arrayElemAt": ["$ON.ON", 0] },
"OFF": { "$arrayElemAt": ["$OFF.OFF", 0] }
}},
])
输出:{ “关”:30 }
expected ouput:{
"OFF" : 30,
"ON":0
}
【问题讨论】:
标签: mongodb mongoose mongodb-query aggregation-framework