Mongodb 管道与之前查找的项目匹配

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

我有一个 mongodb 查询进行管道查找,结果存储为键名称“A”下的新数组,我找不到如何添加另一个管道以在键“B”下注入另一个查找的示例,但使用与第一次查找中的字段的匹配。 就像是 从集合 ROOT 中选择所有文档, 添加集合 A 中的文档(其中 A.id=ROOT.id)作为数组 A,并添加集合 B 中的文档(其中 A.x=B.x)作为数组 A.B

具有所需输出的集合示例:

ROOT {"_id":1,"type":"customer","name":"anton"}
A {"_id":33, "customer":1, "type":"order","address":"azores island"}
B {"_id":"1_33_1", "type":"item","name":"golf ball"}

and want to achieve this output. the id incollection B is in the format of customerId_orderId_itemIndex, a string join

{"_id":1,"type":"customer","name":"anton",
"orders": [
    {
    "_id":33, "customer":1, "type":"order","address":"azores island", 
    "items":[{"_id":"33_1", "type":"item","name":"golf ball"}]
    }
]}
mongodb mongodb-query
1个回答
0
投票

集合中的id

B
的格式为
 customerId_orderId_itemIndex
,字符串连接

这使得连接数据的工作变得更加复杂。相关地,对于我来说,为什么要将信息存储在集合

B
中,与
A
中的订单分开,而不是将它们嵌入到数组中,这对我来说并不是很明显。就我个人而言,我强烈建议至少考虑这一点,因为它可以大大简化当前和未来的开发。

尽管如此,您可以通过类似于以下内容来实现问题中所述的目标:

db.ROOT.aggregate([
  {
    "$lookup": {
      "from": "A",
      "localField": "_id",
      "foreignField": "customer",
      "as": "orders",
      pipeline: [
        {
          "$lookup": {
            "from": "B",
            let: {
              regex: {
                "$concat": [
                  "^",
                  {
                    $toString: "$customer"
                  },
                  "_",
                  {
                    "$toString": "$_id"
                  }
                ]
              }
            },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $regexMatch: {
                      input: "$_id",
                      regex: "$$regex"
                    }
                  }
                }
              }
            ],
            "as": "items"
          }
        }
      ]
    }
  }
])

我们在这里:

  1. $lookup
    ROOT
    之间执行(简单)
    A
    以获取相关
    orders
    文档。
  2. 嵌套在该操作中,我们在
    $lookup
    A
    之间执行另一个
    B
    来获取相关的
    items
    文档。
    • 值得注意的是,这里我们必须跳过一些环节来构建一个正则表达式模式,我们可以在与
      _id
      中的文档的
      B
      字段进行匹配时使用它。

使用以下示例数据:

"ROOT": [
    {
      "_id": 1,
      "type": "customer",
      "name": "anton"
    }
  ],
  "A": [
    {
      "_id": 33,
      "customer": 1,
      "type": "order",
      "address": "azores island"
    }
  ],
  "B": [
    {
      "_id": "1_33_1",
      "type": "item",
      "name": "golf ball"
    },
    {
      "_id": "1_33_2",
      "type": "item",
      "name": "golf club"
    },
    {
      "_id": "2_33_1",
      "type": "item",
      "name": "golf ball"
    }
  ]

输出为:

[
  {
    "_id": 1,
    "name": "anton",
    "orders": [
      {
        "_id": 33,
        "address": "azores island",
        "customer": 1,
        "items": [
          {
            "_id": "1_33_2",
            "name": "golf club",
            "type": "item"
          },
          {
            "_id": "1_33_1",
            "name": "golf ball",
            "type": "item"
          }
        ],
        "type": "order"
      }
    ],
    "type": "customer"
  }
]

这个游乐场示例中了解它是如何工作的

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