我有一个看起来像这样的公司集合。
{
"_id" : ObjectId("5a897da6dffe7eb1a3028863"),
"name" : "ABC",
"website" : "http://www.abc.af",
"alternativeEmail" : "abc@gmail.com",
"address" : "Floor 1, Street 6",
"type" : {
"id" : ObjectId("5a897c81dffe7eb1a302885d"),
"name" : "Company / Corporation"
},
"createdBy" : {
"id" : ObjectId("5a897a21739589bbf635c9b4"),
"firstName" : "Admin",
"lastName" : "Admin"
},
"status" : 1,
"socialLinks" : [],
"__v" : 0
}
另一个集合companyType
,其中包含。
{
"_id" : ObjectId("5a897c81dffe7eb1a302885d"),
"updatedAt" : ISODate("2018-02-20T05:16:09.503Z"),
"createdAt" : ISODate("2018-02-18T13:15:45.624Z"),
"name_en" : "Company / Corporation",
"name_da" : "Company / Corporation",
"name_pa" : "Company / Corporation",
"validPlans" : {
"planId" : ObjectId("5a897b58dffe7eb1a3028858"),
"planName" : "Company / Corporation",
"billingType" : "Prepaid"
},
"__v" : 0
}.
现在,我想要的是过滤companyType.validPlan.billingType === 'prePaid'
company.type.id = companyType._id
。
我写了这样的查询。
db.getCollection('companies').aggregate([
{
$lookup: {
"from": "companytypes",
"localField": "type.id",
"foreignField": "_id",
"as": "companyType"
}
}
]);
我不确定它是否是解决方案,因为我无法按companyType.validPlan.billingType ==='prePaid'
进行过滤。
那我怎么能实现呢?