Collection store 包含嵌套的文档.使用聚合框架,我希望从store 获得所有文档,其中"Products"字段中的UID将被集合产品中的实际产品文档替换.

我有两个这样的Collection :

// 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",
  }
]

Desired outcome:

[
  {
    "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",
                  }
                ]
              },
            ]
          }
        ]
      }
    ]
  }
]

我按照描述的here in the mongo docstry 了$lookup,但对于这种嵌套的文档 struct 不起作用.

谢谢你的帮忙!

推荐答案

如果你真想这么做,一个 Select 是:

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号公路上是如何工作的

But:

我会考虑将 struct 更改 for each store 、国家、城市和地区的文档,其中只有产品是一个array.就像这样:

{
    "_id": ObjectId("5a934e000102030405000000"),
    "name": "Shop A",
    "countryName": "Germany",
    "cityName": "Berlin",
    "districtName": "First District",
    "products": [1, 2, 3]
}

其中可以使用查询:

db.Shops.aggregate([
  {$lookup: {
      from: "Products",
      localField: "products",
      foreignField: "uid",
      as: "res"
  }},
  {$set: {
      products: {$map: {
          input: "$products",
          in: {$arrayElemAt: ["$res", {$indexOfArray: ["$res.uid", "$$this"]}]}
      }},
      res: "$$REMOVE"
  }}
])

Mongodb相关问答推荐

Mongo聚合项目数组交集

如何联接Mongoose中的集合并检索特定字段

Mongo聚合的具体格式

创建索引需要很长时间

使用绝对类型在 Typescript 中编写 Mongoose 的类型化模型和模式的类和接口

如何在 Mongoose 中定义一个通用的嵌套对象

如何根据_id删除文档?

在mongoose中添加多个验证

带有 mongodb 和 nodejs 的实时 Web 应用程序

从 mongodb 中的副本配置重新开始

django 和 mongodb 是否使迁移成为过go ?

.NET 中的 Mongodb 单元测试

在 Mongoose 中填写所有必填字段

请按语法排序 Mongoid Scope

Mongoose 连接认证失败

MongoDB中超过2GB的数据库

使用已排序的数据获取不同的值

如何从mongoose中的对象 ID 获取创建日期?

MongoDB:删除唯一约束

在 mongodb 集合中查找最旧/最新的帖子