用其他集合中的文档替换嵌套文档数组中的值

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

集合 Shops 包含嵌套文档。使用聚合框架,我想从商店获取所有文档,其中“产品”字段中的 UID 被替换为产品集合中的实际产品文档。

我有两个这样的收藏:

// collection Shops
[
  {
    "name": "Shop A",
    "countries": [
      {
        "name": "Germany",
        "cities": [
          {
            "name": "Berlin",
            "districts": [
              {
                "name": "First District",
                "products": [1, 2, 3]
              },
            ]
          }
        ]
      }
    ]
  }
]

//  collection Products
[
  {
    "uid": 1,
    "name": "Banana",
  },
  {
    "uid": 2,
    "name": "Apple",
  },
  {
    "uid": 2,
    "name": "Peach",
  }
]

期望的结果:

[
  {
    "name": "Shop A",
    "countries": [
      {
        "name": "Germany",
        "cities": [
          {
            "name": "Berlin",
            "districts": [
              {
                "name": "First District",
                "products": [
                  {
                    "uid": 1,
                    "name": "Banana",
                  },
                  {
                    "uid": 2,
                    "name": "Apple",
                  },
                  {
                    "uid": 2,
                    "name": "Peach",
                  }
                ]
              },
            ]
          }
        ]
      }
    ]
  }
]

我尝试了

$lookup
,如 mongo 文档中所述,但这不适用于此嵌套文档结构。

感谢您的帮助!

mongodb mongodb-query aggregation-framework
1个回答
0
投票

如果你真的想这样做,一个选择是:

db.Shops.aggregate([
  {
    $lookup: {
      from: "Products",
      localField: "countries.cities.districts.products",
      foreignField: "uid",
      as: "res"
    }
  },
  {
    $project: {
      countriesA: {
        $map: {
          input: "$countries",
          as: "country",
          in: {
            $mergeObjects: [
              "$$country",
              {
                cities: {
                  $map: {
                    input: "$$country.cities",
                    as: "city",
                    in: {
                      $mergeObjects: [
                        "$$city",
                        {
                          districts: {
                            $map: {
                              input: "$$city.districts",
                              as: "district",
                              in: {
                                $mergeObjects: [
                                  "$$district",
                                  {
                                    products: {
                                      $map: {
                                        input: "$$district.products",
                                        in: {
                                          $arrayElemAt: [
                                            "$res",
                                            {
                                              $indexOfArray: [
                                                "$res.uid",
                                                "$$this"
                                              ]
                                            }
                                          ]
                                        }
                                      }
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
])

查看它在 mongoDB Playground 上的工作原理

但是:

我会考虑将结构更改为每个商店、国家、城市和地区的文档,其中只有产品是数组。

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