我想使用mongodb的$ lookup功能

时间:2016-04-29 15:52:17

标签: mongodb

product[{ 
    "_id" : ObjectId("5720cc7d6e589904b855cc12"), 
    "p_id" : "p0001", 
    "p_name" : "printed_pant", 
    "p_company" : "Hoffman", 
    "p_price" : 3677.0, 
    "p_color" : "charcol", 
    "p_size" : "XL", 
    "total_price" : 77217.0, 
    "total_sell" : NumberInt(21)
}]

branch[{ 
    "_id" : {
        "c_branch_id" : "b03", 
        "c_branch_location" : "Hydrabad", 
        "p_id" : "p0001",
        "p_name" : "kurty_pajam"
    }, 

    "total_item" : NumberInt(8)
}]

如何在上述集合中使用$lookup? 产品是左集合,分支是右集合。

1 个答案:

答案 0 :(得分:0)

这样的东西?

db.product.aggregate([
    {
      $lookup:
        {
          from: "branch",
          localField: "p_id",
          foreignField: "_id.p_id",
          as: "branch_docs"
        }
   }
])