【问题标题】:Get info from another collection based of _id and merge - MongoDB从另一个基于 _id 的集合中获取信息并合并 - MongoDB
【发布时间】:2020-11-04 09:56:01
【问题描述】:

我有两个系列。让我们打电话给一个baskets 和另一个fruits

baskets 我们有以下文档:

[{
    basket_name: "John's Basket",
    items_in_basket: [
        {
            fruit_id: 1,
            comment: "Delicious!"
        },
        {
            fruit_id: 2,
            comment: "I did not like this"
        }
    ]
}]

fruits 我们有以下文件:

[{
    _id: 1,
    fruit_name: "Strawberry",
    color: "Red"
},
{
    _id: 2,
    fruit_name: "Watermelon",
    color: "Green"
}]

如何获取John's Basket 中每种水果的信息?

结果应该是这样的:

[{
    fruit_id: 1,
    comment: "Delicious!",
    fruit_name: "Strawberry",
    color: "Red"
},
{
    fruit_id: 2,
    comment: "I did not like this",
    fruit_name: "Watermelon",
    color: "Green"  
}]

【问题讨论】:

  • MongoDB 中没有“加入”。您可以考虑使用 MapReduce 函数来创建新结构,或者编写必要的代码来按需获取每个 fruit 实例并将其与 basket 文档合并到您的客户端代码中。您可能会发现 this Q/A 很有帮助。
  • @WiredPrairie:这应该是一个答案而不是评论,我会赞成;-)
  • @hereandnow78 - 完成。
  • @WiredPrairie 赞成 ;-)

标签: node.js mongodb aggregation-framework


【解决方案1】:

MongoDB 中没有“加入”。你可以:

  • 考虑使用 MapReduce 函数创建一个包含合并数据的新结构
  • 编写按需获取每个fruit 实例所需的代码,并将其与basket 文档合并到您的客户端代码中。
  • 对数据进行非规范化,并在basket 文档中包含每种水果的详细信息。这带来了它自己的一系列问题,因为数据是重复的,然后需要对集合中的每次使用进行更新到特定的 fruit

两者各有利弊。

您可能会发现此 Q/A 很有帮助,还有 this MongoDB 文档。

【讨论】:

    【解决方案2】:

    这不再是真的。

    从 3.2 版开始,MongoDB 添加了 $lookup 命令。

    https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

    db.orders.insert([
       { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },
       { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 },
       { "_id" : 3  }
    ])
    
    db.inventory.insert([
       { "_id" : 1, "sku" : "almonds", description: "product 1", "instock" : 120 },
       { "_id" : 2, "sku" : "bread", description: "product 2", "instock" : 80 },
       { "_id" : 3, "sku" : "cashews", description: "product 3", "instock" : 60 },
       { "_id" : 4, "sku" : "pecans", description: "product 4", "instock" : 70 },
       { "_id" : 5, "sku" : null, description: "Incomplete" },
       { "_id" : 6 }
    ])
    
    
    db.orders.aggregate([
       {
         $lookup:
           {
             from: "inventory",
             localField: "item",
             foreignField: "sku",
             as: "inventory_docs"
           }
      }
    ])
    

    返回:

    {
       "_id" : 1,
       "item" : "almonds",
       "price" : 12,
       "quantity" : 2,
       "inventory_docs" : [
          { "_id" : 1, "sku" : "almonds", "description" : "product 1", "instock" : 120 }
       ]
    }
    {
       "_id" : 2,
       "item" : "pecans",
       "price" : 20,
       "quantity" : 1,
       "inventory_docs" : [
          { "_id" : 4, "sku" : "pecans", "description" : "product 4", "instock" : 70 }
       ]
    }
    {
       "_id" : 3,
       "inventory_docs" : [
          { "_id" : 5, "sku" : null, "description" : "Incomplete" },
          { "_id" : 6 }
       ]
    }
    

    【讨论】:

    猜你喜欢
    • 2022-01-19
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多