Node.js 如何更改MongoDB输出JSON格式
我是MongoDb的新手,希望您能帮我解答这个问题。我编写了以下聚合管道Node.js 如何更改MongoDB输出JSON格式,node.js,mongodb,mongoose,mongodb-query,aggregation-framework,Node.js,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我是MongoDb的新手,希望您能帮我解答这个问题。我编写了以下聚合管道 db.collection1.aggregate([ { "$match": { "type" : "L" }}, { "$facet": { "ON": [ { "$match" : {"lampStatus":'ON'}}, { "$count": "ON" } ], "OFF": [
db.collection1.aggregate([
{ "$match": { "type" : "L" }},
{ "$facet": {
"ON": [
{ "$match" : {"lampStatus":'ON'}},
{ "$count": "ON" }
],
"OFF": [
{ "$match" : {"lampStatus": 'OFF'}},
{ "$count": "OFF" }
]
} },
{ "$project": {
"ON": { "$ifNull": [{ "$arrayElemAt": ["$ON.ON", 0] }, 0 ] },
"OFF": { "$ifNull": [{ "$arrayElemAt": ["$OFF.OFF", 0] }, 0 ] },
} }
])
得到了这种类型的输出
{
"ON" : 0.0,
"OFF": 30
}
但是如何使用json格式获得这种类型的输出呢
/* 1 */
{
"_id": "ON",
"count" : 0.0
}
/* 2 */
{
"_id": "OFF",
"count" : 30.0
}
有谁能推荐我吗
实际产出
{
"ON" : 0.0,
"OFF" : 30
}
预期输出:
{
"_id" : "ON",
"COUNT" : 0.0
}
/* 2 */
{
"_id" : "OFF",
"COUNT" : 30.0
}
升级
db.collection.aggregate([
{ "$match": { "type" : "L" }},
{ "$facet": {
"ON": [
{ "$match" : {"lampStatus":'ON'}},
{ "$count": "ON" }
],
"OFF": [
{ "$match" : {"lampStatus": 'OFF'}},
{ "$count": "OFF" }
]
} },
{"$project": {
myArray: [
{_id:"ON", count: { "$ifNull": [{ "$arrayElemAt": ["$ON.ON", 0] }, 0 ]} },
{_id:"OFF", count: { "$ifNull": [{ "$arrayElemAt": ["$OFF.OFF", 0] }, 0 ] }}
]
}},
{"$unwind":"$myArray"},
{
"$replaceRoot": { newRoot: "$myArray" }
}
])
升级
db.collection.aggregate([
{ "$match": { "type" : "L" }},
{ "$facet": {
"ON": [
{ "$match" : {"lampStatus":'ON'}},
{ "$count": "ON" }
],
"OFF": [
{ "$match" : {"lampStatus": 'OFF'}},
{ "$count": "OFF" }
]
} },
{"$project": {
myArray: [
{_id:"ON", count: { "$ifNull": [{ "$arrayElemAt": ["$ON.ON", 0] }, 0 ]} },
{_id:"OFF", count: { "$ifNull": [{ "$arrayElemAt": ["$OFF.OFF", 0] }, 0 ] }}
]
}},
{"$unwind":"$myArray"},
{
"$replaceRoot": { newRoot: "$myArray" }
}
])
如果你需要更多的聚合技巧
db.collection1.aggregate([
{ "$match": { "type" : "L" }},
{ "$facet": {
"ON": [{ "$match" : {"lampStatus": "ON" }}, { "$count": "ON" }],
"OFF": [{ "$match" : {"lampStatus": "OFF" }}, { "$count": "OFF" }]
}},
{ "$project": {
"ON": { "$ifNull": [{ "$arrayElemAt": ["$ON.ON", 0] }, 0 ] },
"OFF": { "$ifNull": [{ "$arrayElemAt": ["$OFF.OFF", 0] }, 0 ] },
}},
{ "$project": {
"data": {
"$map": { "input": { "$objectToArray": "$$ROOT" }, "in": { "_id": "$$this.k", "count": "$$this.v" }}
}
}},
{ "$unwind": "$data" },
{ "$replaceRoot": { "newRoot": "$data" }}
])
如果你需要更多的聚合技巧
db.collection1.aggregate([
{ "$match": { "type" : "L" }},
{ "$facet": {
"ON": [{ "$match" : {"lampStatus": "ON" }}, { "$count": "ON" }],
"OFF": [{ "$match" : {"lampStatus": "OFF" }}, { "$count": "OFF" }]
}},
{ "$project": {
"ON": { "$ifNull": [{ "$arrayElemAt": ["$ON.ON", 0] }, 0 ] },
"OFF": { "$ifNull": [{ "$arrayElemAt": ["$OFF.OFF", 0] }, 0 ] },
}},
{ "$project": {
"data": {
"$map": { "input": { "$objectToArray": "$$ROOT" }, "in": { "_id": "$$this.k", "count": "$$this.v" }}
}
}},
{ "$unwind": "$data" },
{ "$replaceRoot": { "newRoot": "$data" }}
])
Iam尝试此代码Iam没有得到确切的结果Iam得到类似{u id:“ON”,“myArray:[0.0,0.0]}/*2*/{u id:“OFF”,“myArray:[0.0,0.0]}的类型结果@abdul@what关于现在?我正在尝试这个{$group:{“\u id”:“$lampStatus”,COUNT:{$sum:1}}},我得到的是json的精确结果,但很小的ISSUE lampstatus:“ON”没有记录,所以没有得到{“\u id”:“ON”,“COUNT”:0.0},value@NareshVarunGuttula更新了我的答案,这是另一种解决方案。非常感谢@abdulIam尝试此代码我没有得到确切的结果我得到了类似{“\u id”:“ON”的类型结果,myArray:[0.0,0.0]}/*2*/{“\u id:“OFF”,“myArray:[0.0,0.0]}@abdul@what关于现在?我正在尝试这个{$group:{“\u id”:“$lampStatus”,COUNT:{$sum:1}}},我得到的是json精确的结果,但很小的是,Issue lampStatus:“ON”没有记录,所以没有得到{“\u id”:“开”,“计数”:0.0},value@NareshVarunGuttula更新了我的答案,这是另一种解决方案。非常感谢@abdulGot it非常感谢@Anthony Winzlet非常感谢@Anthony Winzlet