英文:
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" }
      }},
     .........,
])
有人可以帮助我吗?
谢谢提前!
英文:
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?
答案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"
  }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论