简体   繁体   English

MongoDB:创建聚合管道

[英]MongoDB: Create an aggregation pipeline

In the MongoDB aggregation framework, I was hoping to use the $unwind operator on a map.在 MongoDB 聚合框架中,我希望在 map 上使用 $unwind 运算符。 Looks like it's not possible.看起来是不可能的。

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

This is the sample JSON where I have matchStatus => Map<String,MatchStatus>这是示例 JSON 我有 matchStatus => Map<String,MatchStatus>

{
    "_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"
}

So the output I want is =>所以我想要的 output 是 =>

{
"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"
}
}

Or something similar to this where I fetch the key (summary, income, slip) and total the values.或者与此类似的东西,我获取密钥(摘要、收入、单据)并汇总值。

Tried $unwind but did not work on the map structure.尝试 $unwind 但在 map 结构上不起作用。

Maybe there is a smaller aggregate也许有一个较小的聚合

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 mongoplayground

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM