从同一集合中填充ID数组。

huangapple go评论66阅读模式
英文:

populate array of ids from same same collection

问题

以下是您要翻译的内容:

我有一组产品,其中我将变体保存为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" }
      }},
     .........,
])

有人可以帮助我吗?

谢谢提前!

从同一集合中填充ID数组。

英文:

I have a collection of products where I've kept variants as an array of ids, but this array of ids are the _ids from the same collection. Now if I want to populate the ids how can I do that.

The collection looks like this.

So I tried to populate this as below but it does not work.

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

Can anyone please help me?

Thanks in advance!
从同一集合中填充ID数组。

答案1

得分: 1

你的$lookup中有一个拼写错误:

{
    "$lookup": {
      "from": "products", // 这里应该是 `products` 而不是 `$products`
      "foreignField": "_id",
      "localField": "variantIdsArr",
      "as": "variantIdsArr"
  }
}
英文:

Your $lookup has a typo:

{
    "$lookup": {
      "from": "$products", // This should be `products` instead of `$products`
      "foreignField": "_id",
      "localField": "variantIdsArr",
      "as": "variantIdsArr"
  }
}

huangapple
  • 本文由 发表于 2023年5月18日 12:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定