【问题标题】:how to change MongoDB output JSON format如何更改 MongoDB 输出 JSON 格式
【发布时间】:2018-09-05 03:24:19
【问题描述】:

我是 MongoDb 的新手,希望能在此查询方面提供一些帮助。我写了以下聚合管道

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
}

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query aggregation-framework


    【解决方案1】:

    如果您需要更多聚合技巧

    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" }}
    ])
    

    【讨论】:

    • 知道了,非常感谢@Anthony Winzlet
    【解决方案2】:

    更新

    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" }
            }
    
    ])
    

    【讨论】:

    • 我试试这个代码我没有得到确切的结果我得到这个类型的结果就像{“_id”:“ON”,“myArray”:[0.0,0.0]}/* 2*/{“_id " : "OFF", "myArray" : [ 0.0, 0.0 ] } @abdul
    • @现在呢?
    • 我正在尝试这个 { $group: { "_id": "$lampStatus",COUNT: { $sum:1} } },我得到了 json 确切的结果,但小问题 lampstatus:"ON " 没有记录所以没有得到 { "_id" : "ON", "COUNT" : 0.0 },value
    • @NareshVarunGuttula 更新了我的答案,这是一种替代解决方案。
    • 非常感谢@abdul
    猜你喜欢
    • 1970-01-01
    • 2017-06-09
    • 2022-01-21
    • 1970-01-01
    • 2019-06-13
    • 2016-03-08
    • 2019-08-19
    • 2019-01-29
    • 1970-01-01
    相关资源
    最近更新 更多