实际上我并不是想要匹配两个系列。我正在寻找与二次收藏相匹配的领域。我会清楚地解释我的问题: 这是我的主要收藏品“bags”。其中包含
{
"_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" }
从这两个系列我想从袋子收集中获得已接受的袋子数据,在捐赠者收藏中将'donortype'作为'H'。如果有可能,请告诉我。解释一些例子
答案 0 :(得分: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'}}
]);