Mongodb管道与先前查找的项匹配

3phpmpom  于 2023-11-17  发布在  Go
关注(0)|答案(1)|浏览(226)

我有一个mongodb查询做管道查找和结果存储为一个新的数组下的关键字“A”,我找不到一个例子,如何添加另一个管道注入另一个查找下的关键字“B”,但使用匹配字段从第一个查找。类似于选择所有文档从集合根,添加文档从集合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"}]
    }
]}

字符串

wlsrxk51

wlsrxk511#

集合B中的id的格式为customerId_orderId_itemIndex,一个字符串连接
这使得连接数据的工作变得更加复杂。与此相关的是,我并不清楚为什么要将信息存储在集合B中,而不是将它们嵌入到数组中。我个人强烈建议至少考虑一下,因为它可以大大简化当前和未来的开发。
然而,你可以通过以下类似的方法来实现你问题中所述的目标:

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.在ROOTA之间执行(简单)$lookup,以获取相关的orders文档。

  • 嵌套在该操作中,我们在AB之间执行另一个$lookup,以获取相关的items文档。
  • 值得注意的是,在这里我们必须跳过一些环来构造一个正则表达式模式,当匹配B中文档的_id字段时,我们可以使用它。

使用以下示例数据:

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


了解它在this playground example中的工作原理

相关问题