可能在一个数组中使用数组进行MongoDb聚合

可能在一个数组中使用数组进行MongoDb聚合,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我正在努力寻找一些使用mongo聚合框架处理文档的示例,这些文档包含一个项目数组,其中每个项目还包含一个其他对象数组(包含一个数组的数组) 在下面的示例文档中,我真正想要的是一个示例,它对文档中所有案例的结果数组中的itemValue求和,并根据文档位置代码对result.decision“接受”的集合进行分组 但是,即使是找到result.decision被“接受”显示的所有文档,或者将itemValue相加的示例也会有所帮助 非常感谢 { "_id": "333212", "data": {

我正在努力寻找一些使用mongo聚合框架处理文档的示例,这些文档包含一个项目数组,其中每个项目还包含一个其他对象数组(包含一个数组的数组)

在下面的示例文档中,我真正想要的是一个示例,它对文档中所有案例的结果数组中的itemValue求和,并根据文档位置代码对result.decision“接受”的集合进行分组

但是,即使是找到result.decision被“接受”显示的所有文档,或者将itemValue相加的示例也会有所帮助

非常感谢

{
"_id": "333212",
"data": {
    "locationCode": "UK-555-5566",
    "mode": "retail",
    "caseHandler": "A N Other",
    "cases": [{
            "caseId": "CSE525666",
            "items": [{
                    "id": "333212-CSE525666-1",
                    "type": "hardware",
                    "subType": "print cartridge",
                    "targetDate": "2020-06-15",
                    "itemDetail": {
                        "description": "acme print cartridge",
                        "quantity": 2,
                        "weight": "1.5"
                    },
                    "result": {
                        "decision": "rejected",
                        "decisionDate": "2019-02-02"
                    },
                    "isPriority": true
                },
                {
                    "id": "333212-CSE525666-2",
                    "type": "Stationery",
                    "subType": "other",
                    "targetDate": "2020-06-15",
                    "itemDetail": {
                        "description": "staples box",
                        "quantity": 3,
                        "weight": "1.66"
                    },
                    "result": {
                        "decision": "accepted",
                        "decisionDate": "2020-03-03",
                        "itemValue": "23.01"
                    },
                    "isPriority": true
                }
            ]
        },
        {
            "caseId": "CSE885655",
            "items": [{
                    "id": "333212-CSE885655-1",
                    "type": "marine goods",
                    "subType": "fish food",
                    "targetDate": "2020-06-04",
                    "itemDetail": {
                        "description": "fish bait",
                        "quantity": 5,
                        "weight": "0.65"
                    },
                    "result": {
                        "decision": "accepted",
                        "decisionDate": "2020-03-02"
                    },
                    "isPriority": false
                },
                {
                    "id": "333212-CSE885655-4",
                    "type": "tobacco products",
                    "subType": "cigarettes",
                    "deadlineDate": "2020-06-15",
                    "itemDetail": {
                        "description": "rolling tobbaco",
                        "quantity": 42,
                        "weight": "2.25"
                    },
                    "result": {
                        "decision": "accepted",
                        "decisionDate": "2020-02-02",
                        "itemValue": "48.15"
                    },
                    "isPriority": true
                }
            ]
        }
    ]
},
"state": "open"

}

您可能正在寻找。它接受文档中的一个数组,并为每个数组成员创建一个单独的文档

{ foos: [1, 2] } -> { foos: 1 }, { foos: 2}
有了它,您可以创建一个平面文档结构,并像往常一样匹配和分组

db.collection.aggregate([
  {
    $unwind: "$data.cases"
  },
  {
    $unwind: "$data.cases.items"
  },
  {
    $match: {
      "data.cases.items.result.decision": "accepted"
    }
  },
  {
    $group: {
      _id: "$data.locationCode",
      value: {
        $sum: {
          $toDecimal: "$data.cases.items.result.itemValue"
        }
      }
    }
  },
  {
    $project: {
      _id: 0,
      locationCode: "$_id",
      value: "$value"
    }
  }
])

替代解决方案。。。 我们按
data.locationCode
分组,并将所有符合此条件的项目相加:
案例[*].项目[*].结果.决定“==”接受“



非常感谢这是一个非常有帮助的答案。我想我也可以用这个例子作为我需要的其他查询的起点。$unwind似乎是一个非常好的解决方案,聚合的例子比我希望的要简洁得多。非常感谢。感谢你提供了这个替代方案。这对我看到一个不同的解决方案非常有帮助d我将能够在构建进一步的查询时使用这些想法和方法。非常感谢
db.collection.aggregate([
  {
    $group: {
      _id: "$data.locationCode",
      itemValue: {
        $sum: {
          $reduce: {
            input: "$data.cases",
            initialValue: 0,
            in: {
              $sum: {
                $concatArrays: [
                  [ "$$value" ],
                  {
                    $map: {
                      input: {
                        $filter: {
                          input: "$$this.items",
                          as: "f",
                          cond: {
                            $eq: [ "$$f.result.decision", "accepted" ]
                          }
                        }
                      },
                      as: "item",
                      in: {
                        $toDouble: {
                          $ifNull: [ "$$item.result.itemValue", 0 ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
])