将两个mongo聚合查询结果合并为一个

问题描述 投票:0回答:1

我有两个输出结果的 mongo 聚合管道。现在我想将这两个管道组合起来以获得单一输出。

请参阅以下样本集。

[
  {
    _id: "ddfdfdfdggfgfgsg",
    rate: "3323",
    quantity_packs: "343",
    shop_name: "Whole Foods",
    sku: "20"
    manufacturer_name: "Unilever"
  },
  {
    _id: "ddfdfdfsdsds",
    rate: "434",
    quantity_packs: "453",
    shop_name: "Carrefour",
    sku: "200"
    manufacturer_name: "Unilever"
  },
  {
    _id: "dfdfdgcvgfgfvvv",
    rate: "343",
    quantity_packs: "23",
    shop_name: "Target",
    manufacturer_name: "Beirsdorf"
    sku: "34"
  }
]

请在下面找到我的疑问。

第一次查询

db.collection.aggregate([
    {
        $match: {
            manufacturer_name: {
                $in: [ "unilever" ]
            }
        }
    }, 

    {
        $group: {
            _id: {
                "Shop Name": "$shop_name"
            },

            "total_sku": {
                "$addToSet": "$sku"
            },

            "annual_cost": {
                $sum: {
                    $cond: [
                        {
                            $eq: ["$manufacturer_name", "unilever"]
                        },
                        {
                            "$toDouble": "$rate"
                        }, 
                        0
                    ]
                }
            },

            "annual_qty": {
                $sum: {
                    "$toDouble": "$annual_qty"
                }
            }
        }
    },

    {
        $project: {
            "sku count": {
                "$size": "$total_sku"
            },

            "Annual Cost WO GST": {
                $multiply: [ "$annual_cost", "$annual_qty" ]
            },
        }
    },
])

第一次查询结果

[
  {
    _id: { 'Hospital Name': '7AM mart' },
    'sku count': 29,
    'Annual Cost WO GST': 79968887.67999999
  },
  {
    _id: { 'Shop Name': 'Apex' },
    'sku count': 20,
    'Annual Cost WO GST': 1779192666.96
  }
]

第二次查询

db.collection.aggregate([
    {
        $match: {
            $expr: {
                $ne: ["$manufacturer_name", "unilever"]
            }
        }
    }, 

    {
        $group: {
            _id: {
                "Shop Name": "$shop_name"
            },
            
            "annual_cost_wo_gst_wo_manu": {
                $sum: {
                    "$toDouble": "$rate"
                }
            },

            "annual_qty": {
                $sum: {
                    "$toDouble": "$annual_qty"
                }
            }
        }
    },

    {
        $project: {
            "Ann Cost For Other Manufacturers": {
                $multiply: ["$annual_cost_wo_gst_wo_manu", "$annual_qty"]
            },
        }
    }
])

第二次查询结果

[
  {
    _id: { 'Hospital Name': 'Apex' },
    'Ann Cost For Other Manufacturers': 25246715130525.273
  },
  {
    _id: { 'Hospital Name': '7AM Mart' },
    'Ann Cost For Other Manufacturers': 1347701834351.495
  }
]

如上所述,我想通过正确映射项目来组合结果。

预期结果

[
      {
        _id: { 'Hospital Name': '7AM mart' },
        'sku count': 29,
        'Annual Cost WO GST': 79968887.67999999
        'Ann Cost For Other Manufacturers': 1347701834351.495
      },
      {
        _id: { 'Shop Name': 'Apex' },
        'sku count': 20,
        'Annual Cost WO GST': 1779192666.96
        'Ann Cost For Other Manufacturers': 25246715130525.273
      }
    ]
mongodb mongodb-query aggregation-framework
1个回答
0
投票

您的 2 个查询并不能完全产生您所说的输出。不过,您可以首先执行不相关的

$lookup
来执行第二个查询,将第二个查询的结果存储在字段/对象中。然后您可以继续您的第一个查询。最后从之前存储的字段/对象中提取二次查询的结果。

db.collection.aggregate([
  {
    $match: {
      manufacturer_name: {
        $in: [
          "Unilever"
        ]
      }
    }
  },
  {
    "$lookup": {
      "from": "collection",
      "let": {
        "id": "$_id"
      },
      "pipeline": [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$_id",
                    "$$id"
                  ]
                },
                {
                  $ne: [
                    "$manufacturer_name",
                    "unilever"
                  ]
                }
              ]
            }
          }
        },
        {
          $group: {
            _id: {
              "Shop Name": "$shop_name"
            },
            "annual_cost_wo_gst_wo_manu": {
              $sum: {
                "$toDouble": "$rate"
              }
            },
            "annual_qty": {
              $sum: {
                "$toDouble": "$quantity_packs"
              }
            }
          }
        },
        {
          $project: {
            "ann_cost_for_other_manufacturers": {
              $multiply: [
                "$annual_cost_wo_gst_wo_manu",
                "$annual_qty"
              ]
            }
          }
        }
      ],
      "as": "secondQueryResult"
    }
  },
  {
    $unwind: "$secondQueryResult"
  },
  {
    $group: {
      _id: {
        "Shop Name": "$shop_name"
      },
      "total_sku": {
        "$addToSet": "$sku"
      },
      "annual_cost": {
        $sum: {
          $cond: [
            {
              $eq: [
                "$manufacturer_name",
                "Unilever"
              ]
            },
            {
              "$toDouble": "$rate"
            },
            0
          ]
        }
      },
      "annual_qty": {
        $sum: {
          "$toDouble": "$quantity_packs"
        }
      },
      "Ann Cost For Other Manufacturers": {
        $first: "$secondQueryResult.ann_cost_for_other_manufacturers"
      }
    }
  },
  {
    $project: {
      "sku count": {
        "$size": "$total_sku"
      },
      "Annual Cost WO GST": {
        $multiply: [
          "$annual_cost",
          "$annual_qty"
        ]
      },
      "Ann Cost For Other Manufacturers": 1
    }
  }
])

这是一个 Mongo Playground,对您的原始示例进行了一些修改,供您参考。

© www.soinside.com 2019 - 2024. All rights reserved.