2016-03-05 57 views
0

其实我不想匹配两个集合的。我正在寻找与二次收集相匹配的领域。我要解释清楚我的问题: 这是我的主要采集“袋”。其中包含Mongodb聚合与多个键的联合声明

{ 
    "_id" : ObjectId("568f43e08a9f71b70b22a694"), 
      "bagNo" : "HBBN/00001/16", 
      "category" : "Voluntary", 
      "regNo" : "DNR/00001/16", 
      "status": "Accepted" 
    }, 
{ "_id" : ObjectId("568f4645fa0758af0e3fef26"), "bagNo" :"HBBN/00002/16", "category" : "Voluntary", "regNo" : "DNR/00002/16", "status": "Rejected" }, 

{ "_id" : ObjectId("568f4645fa4546gygfef26"), "bagNo" : "HBBN/00003/16", "category" : "Voluntary", "regNo" : "DNR/00003/16", "status": "Accepted" } 

现在我加入收藏“供体”包含

{"donorType" : "H", 
    "regNo" : "DNR/00001/16", 
    "surName" : "pandey", 
    "firstName" : "rakesh"}, 
{"donorType" : "C", "regNo" : "DNR/00002/16", "surName" : "pandey", "firstName" : "rakesh" }, 

{"donorType" : "C", "regNo" : "DNR/00003/16", "surName" : "pandey", "firstName" : "rakesh" } 
从这两集我想

在捐赠者collection.Please包含'donortype'为'H'的可接受袋数据。请告诉我是否有可能。例如

回答

0

要合并两个收藏品,您可以尝试使用$lookup在聚合中灰。

donor.aggregation([ 
     // filter the `donortype` of `H` 
     {$match: {donortype: 'H'}}, 
     // join with `bags` collection for the foreign key `regNo`, and put bags into `h_bags` result. 
     {$lookup: { 
      from: "bags", 
      localField: "regNo", 
      foreignField: "regNo", 
      as: "h_bags" 
     }}, 
     // unwind the `h_bags` array 
     {$unwind: '$h_bags'}, 
     // filter the status of bags is `Accepted` 
     {$match: {'h_bags.status': 'Accepted'}} 
    ]); 
+0

但我想只接受包,即“状态”从袋收集“接受”。为此,我应该在上面的代码中保留条件。是否有可能只写联合收集声明? –

+0

@SivaM,请看我更新的答案。你可以'$ unwind'结果'u_bags'数组母鸡用'status'过滤它''Accepted' – zangw

+0

非常感谢你@Zangw。你解决了我的问题。还有一个问题?我可以加入两个以上的收藏品吗?如果有可能,你可以向我解释一下如何在当前的mongo版本中加入 –