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

时间:2021-07-21 13:29:41

标签: mongodb mongodb-query aggregation-framework

我有两个输出结果的 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
      }
    ]

1 个答案:

答案 0 :(得分:0)

您的 2 个查询并不能完全产生您声明的输出。不过,您可以先执行不相关的 $lookup 来执行第二个查询,将第二个查询的结果存储在字段/对象中。然后您可以继续您的第一个查询。最后从之前存储的字段/对象中提取二次查询的结果。

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

相关问题