代码之家  ›  专栏  ›  技术社区  ›  Adrien Chapelet

MongoDB聚合与阵列内的外部模型

  •  0
  • Adrien Chapelet  · 技术社区  · 2 年前

    我试图用一个外国模型来聚合数据。 我尝试增压的结构如下:

    {
        "_id" : ObjectId("62b489664cbb9bc8c947f19f"),
        "user_id" : ObjectId("61a775da4cbb9bc8c947edd9"),
        "product_types" : [ 
            {
                "type" : NumberLong(1),
                "product_id" : ObjectId("62b4890f4cbb9bc8c947e5ef"),
            }, 
            {
                "type" : NumberLong(1),
                "product_id" : ObjectId("62b4890f4cbb9bc8c947e5ed"),
            }
        ]
    }
    

    我试图从product_id添加产品数据,我认为我与之非常接近,但我在一个数组中添加了两个相同的产品,而不是正确的产品: 查询:

    db.getCollection('interests').aggregate([
        {
            $lookup:{
                from: "products",
                localField: "product_types.product_id",
                foreignField: "_id",
                as: "productInterestData"
            }
        },
        {
          $set: {
            "product_types.product": {
              $map: {
                input: "$product_types",
                in: {
                  $mergeObjects: [
                    "$this",
                    {
                        $arrayElemAt: [
                          "$productInterestData",
                          {$indexOfArray: ["$productInterestData.id", "$this.id"]}
                        ]
                    }
                  ]
                }
              }
            }
          }
        },
        {$unset: "productInterestData"}
      ])
    

    结果(使用2个相同产品的数组,而不是正确的产品):

    {
        "_id" : ObjectId("62b489664cbb9bc8c947f19f"),
        "user_id" : ObjectId("61a775da4cbb9bc8c947edd9"),
        "product_types" : [ 
            {
                "type" : NumberLong(0),
                "product_id" : ObjectId("62b4890f4cbb9bc8c947e5ef"),
                "product" : [ 
                    {
                        "_id" : ObjectId("62b4890f4cbb9bc8c947e5ef"),
                        "name" : "olive",
                    }, 
                    {
                        "_id" : ObjectId("62b4890f4cbb9bc8c947e5ef"),
                        "name" : "olive",
                    }
                ]
            }, 
            {
                "type" : NumberLong(1),
                "product_id" : ObjectId("62b4890f4cbb9bc8c947e5ed"),
                "product" : [ 
                    {
                        "_id" : ObjectId("62b4890f4cbb9bc8c947e5ef"),
                        "name" : "olive",
                    }, 
                    {
                        "_id" : ObjectId("62b4890f4cbb9bc8c947e5ef"),
                        "name" : "olive",
                    }
                ]
            }
        ]
    }
    

    你知道如何修复查询,使其只有一个产品,而不是一系列相同的产品吗?

    1 回复  |  直到 2 年前
        1
  •  1
  •   nimrod serok    2 年前

    $set 阶段:

    1. product_types product_types.product ,以避免阵列重复。为了把它套在一起 product 添加密钥 产品 $mergeObjects 活动
    2. $productInterestData._id 而不是 $productInterestData.id
    3. $$this 而不是 $this (我们需要两个 $ 此处)
    4. $$this.product_id 而不是 $this.id
    db.interests.aggregate([
      {
        $lookup: {
          from: "products",
          localField: "product_types.product_id",
          foreignField: "_id",
          as: "productInterestData"
        }
      },
      {
        $set: {
          product_types: {
            $map: {
              input: "$product_types",
              in: {
                $mergeObjects: [
                  "$$this",
                  {product:{
                    $arrayElemAt: [
                      "$productInterestData",
                      {$indexOfArray: ["$productInterestData._id", "$$this.product_id"]}
                    ]
                  }}
                ]
              }
            }
          }
        }
      },
      {$unset: "productInterestData"}
    ])
    

    查看它在 playground example