填充来自同一集合的 id 数组

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

我有一个产品集合,其中我将变体保存为一个 id 数组,但是这个 id 数组是来自同一个集合的

_ids
。现在,如果我想填充 ID,我该怎么做。

合集长这样

所以我尝试如下填充它,但它不起作用。

db.products.aggregate([
     ..........,
     {"$unwind": "$variantIdsArr" },
      { "$lookup": {
        "from": "$products",
        "foreignField": "_id",
        "localField": "variantIdsArr",
        "as": "variantIdsArr"
      }},
      { "$unwind": "$variantIdsArr" },
      { "$group": {
        "_id": "$_id",
        "variantIdsArr": { "$push": "$variantIdsArr" }
      }},
     .........,
])

任何人都可以帮助我吗?

提前致谢!

node.js mongodb mongoose aggregation-framework
1个回答
0
投票

您的

$lookup
打字错误:

{
    "$lookup": {
      "from": "$products", // This should be `products` instead of `$products`
      "foreignField": "_id",
      "localField": "variantIdsArr",
      "as": "variantIdsArr"
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.