【问题标题】:MongoDB: Create an aggregation pipelineMongoDB:创建聚合管道
【发布时间】:2022-01-20 07:54:48
【问题描述】:

在 MongoDB 聚合框架中,我希望在地图上使用 $unwind 运算符。看来是不可能了。

case class MatchStatus(
    totalRows: Int,
    fullMatch: Int,
    noMatch: Int,
    partialMatch: Int
)

这是我有 matchStatus => Map

的示例 JSON
{
    "_id" : ObjectId("61e8c7bbd0597207179faa89"),
    "clientId" : "DEMO",
    "matchStatus" : {
        "summary" : {
            "totalRows" : "10",
            "fullMatch" : "5",
            "noMatch" : "1",
            "partialMatch" : "4"
        },
        "income" : {
            "totalRows" : "10",
            "fullMatch" : "1",
            "noMatch" : "0",
            "partialMatch" : "1"
        }
    },
    "date" : "18-01-2022"
},
{
    "_id" : ObjectId("61e8c7bbd0597207179faa89"),
    "clientId" : "DEMO-1",
    "sizes" : [ 
        "1", 
        "2"
    ],
    "matchStatus" : {
        "summary" : {
            "totalRows" : "10",
            "fullMatch" : "5",
            "noMatch" : "1",
            "partialMatch" : "4"
        },
        "income" : {
            "totalRows" : "10",
            "fullMatch" : "1",
            "noMatch" : "0",
            "partialMatch" : "1"
        },
        "slip" : {
            "totalRows" : "10",
            "fullMatch" : "1",
            "noMatch" : "0",
            "partialMatch" : "1"
        },
    },
    "date" : "18-01-2022"
}

所以我想要的输出是 =>

{
"summary":{
     "totalRows" : "20",
      "fullMatch" : "10",
      "noMatch" : "2",
      "partialMatch" : "8"
},
"income":{
     "totalRows" : "20",
      "fullMatch" : "10",
      "noMatch" : "0",
      "partialMatch" : "2"
},
"slip":{
     "totalRows" : "10",
      "fullMatch" : "1",
      "noMatch" : "0",
      "partialMatch" : "1"
}
}

或类似的东西,我获取密钥(摘要、收入、单据)并汇总值。

尝试了 $unwind 但在地图结构上不起作用。

【问题讨论】:

    标签: database mongodb nosql nosql-aggregation


    【解决方案1】:

    也许有一个较小的聚合

    db.collection.aggregate([
      {
        "$match": {
          "_id": {
            "$in": [
              ObjectId("61e8c7bbd0597207179faa89"),
              ObjectId("61e8c7bbd0597207179faa90")
            ]
          }
        }
      },
      {
        "$set": {
          "matchStatus": {
            $objectToArray: "$matchStatus"
          }
        }
      },
      {
        "$unwind": "$matchStatus"
      },
      {
        "$replaceRoot": {
          "newRoot": "$matchStatus"
        }
      },
      {
        "$set": {
          "v": {
            $objectToArray: "$v"
          }
        }
      },
      {
        "$unwind": "$v"
      },
      {
        "$group": {
          "_id": {
            k1: "$k",
            k2: "$v.k"
          },
          "sum": {
            "$sum": {
              "$toInt": "$v.v"
            }
          }
        }
      },
      {
        "$group": {
          "_id": "$_id.k1",
          "field": {
            "$push": {
              k: "$$ROOT._id.k2",
              v: "$$ROOT.sum"
            }
          }
        }
      },
      {
        "$project": {
          "v": {
            $arrayToObject: "$field"
          }
        }
      },
      {
        "$group": {
          "_id": null,
          "field": {
            "$push": {
              k: "$$ROOT._id",
              v: "$$ROOT.v"
            }
          }
        }
      },
      {
        "$replaceRoot": {
          "newRoot": {
            $arrayToObject: "$field"
          }
        }
      }
    ])
    

    mongoplayground

    【讨论】:

      猜你喜欢
      • 2022-12-09
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      • 2020-09-20
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      相关资源
      最近更新 更多