带有查找管道的双重嵌套聚合

Double nested aggregation with lookup pipeline

大家好,我有 3 个 collection 如下所示。我需要从展示柜 collection 中汇总并将产品放入其中,并且对于每个我想要匹配每个折扣的产品。我有一个如下所示的聚合,我认为它在 ids 上损坏了。

我失败的聚合

            {
                $lookup: {
                    from: 'products',
                    let: { products: '$products' },
                    pipeline: [
                        { $match: { $expr: { $in: ['$_id', '$$products'] } } },
                        {
                            $lookup: {
                                from: 'discounts',
                                let: { discounts: '$discounts' },
                                pipeline: [{ $match: { $expr: { $in: ['$_id', '$$discounts'] } } }],
                                as: 'discounts'
                            }
                        }
                    ],
                    as: 'products'
                }
            }

展示Collection

{
    "_id": {
        "$oid": "61f16fdc62c4d2deead39216"
    },
    "title": "Exclusive Offer",
    "products": [{
        "$oid": "61ee8df32bd310de954a2712"
    }, {
        "$oid": "61ee8da12bd310de954a2708"
    }, {
        "$oid": "61ee8e162bd310de954a2718"
    }],
    "categories": [],
    "__v": 0
}

产品Collection

{
    "_id": {
        "$oid": "61ee8da12bd310de954a2708"
    },
    "name": "Sprite Can",
    "shortDescription": "325ml, Price",
    "description": "",
    "price": 4.99,
    "images": [{
        "$oid": "61ee8c992bd310de954a26f4"
    }],
    "labels": [{
        "$oid": "61ee8d732bd310de954a2704"
    }],
    "freeShipping": true,
    "stock": {
        "available": true,
        "total": 42
    },
    "category": {
        "$oid": "61ee8bb32bd310de954a26ef"
    },
    "status": true,
    "nutritions": {
        "Fat": "120g",
        "Iron": "0.3mg"
    },
    "__v": 0
}

折扣Collection

{
    "_id": {
        "$oid": "61f3c19204fcf9025febf148"
    },
    "productId": {
        "$oid": "61ee8da12bd310de954a2708"
    },
    "discountPrice": 3.99,
    "__v": 0
}

需要回复

{
    "_id": {
        "$oid": "61f16fdc62c4d2deead39216"
    },
    "title": "Exclusive Offer",
    "products": [{
    "_id": {
        "$oid": "61ee8da12bd310de954a2708"
    },
    "name": "Sprite Can",
    "shortDescription": "325ml, Price",
    "description": "",
    "price": 4.99,
    "images": [{
        "$oid": "61ee8c992bd310de954a26f4"
    }],
    "labels": [{
        "$oid": "61ee8d732bd310de954a2704"
    }],
    "freeShipping": true,
    "stock": {
        "available": true,
        "total": 42
    },
    "category": {
        "$oid": "61ee8bb32bd310de954a26ef"
    },
    "status": true,
    "nutritions": {
        "Fat": "120g",
        "Iron": "0.3mg"
    },
    "__v": 0
    "discountPrice": 3.99 // FIELD TO BE ADDED
}],
    "categories": [],
    "__v": 0
}
db.Showcase.aggregate([
  {
    $lookup: {
      from: "Product",
      let: { products: "$products" },
      pipeline: [
        {
          $match: { $expr: { $in: [ "$_id", "$$products" ] } }
        },
        {
          $lookup: {
            from: "Discount",
            localField: "_id",
            foreignField: "productId",
            as: "discountPrice"
          }
        },        
        {
          $set: { "discountPrice": { $first: "$discountPrice.discountPrice"} }
        }
      ],
      as: "products"
    }
  }
])

mongoplayground