我正在尝试使用$lookup
进行聚合。我的“引号”包含每个引用的文档,如下所示
{
"_id" : "11223344",
"_etag" : ObjectId("58b7fe8c3df03c1594ab0c45"),
"vehicles" : [
"1122334455",
"1122334456"
]
}
现在车辆是另一个集合,如下所示
{
"_id" : "1122334455",
"referenceNumber" : "11223344",
"sequenceNumber" : 15,
"status" : {
"statusCode" : "consectetur amet fugiat id",
"statusDesc" : "exercitation"
},
"trackerInstalled" : true,
"vehicleDetails" : {
"year" : "2012",
"make" : "Toyota",
"model" : "Corrola"
},
"riskPremium" : "99.00",
"_etag" : ObjectId("58b91f6d1cdd923b3ce7f091")
}
{
"_id" : "1122334456",
"referenceNumber" : "11223344",
"sequenceNumber" : 16,
"status" : {
"statusCode" : "consectetur amet fugiat id",
"statusDesc" : "exercitation"
},
"trackerInstalled" : true,
"vehicleDetails" : {
"year" : "2012",
"make" : "BMW",
"model" : "320d"
},
"riskPremium" : "199.00"
}
我想要的是当用户查询为GET / quotes / 1122334455时,我应该向他发送一份报价文件,其中所有车辆都来自车辆集合,如下所示,
{
"_id" : "11223344",
"_etag" : ObjectId("58b7fe8c3df03c1594ab0c45"),
"vehicles" : "1122334455",
"vehicle_doc" : [
{
"_id" : "1122334455",
"referenceNumber" : "11223344",
"sequenceNumber" : 15,
"status" : {
"statusCode" : "consectetur amet fugiat id",
"statusDesc" : "exercitation"
},
"trackerInstalled" : true,
"vehicleDetails" : {
"year" : "2012",
"make" : "Toyota",
"model" : "Corrola"
},
"riskPremium" : "99.00",
"_etag" : ObjectId("58b91f6d1cdd923b3ce7f091")
},
{
"_id" : "1122334456",
"referenceNumber" : "11223344",
"sequenceNumber" : 16,
"status" : {
"statusCode" : "consectetur amet fugiat id",
"statusDesc" : "exercitation"
},
"trackerInstalled" : true,
"vehicleDetails" : {
"year" : "2012",
"make" : "BMW",
"model" : "320d"
},
"riskPremium" : "199.00"
}
]
}
我使用了以下聚合
db.quotes.aggregate([
{ $unwind: "$vehicles" },
{ $lookup: {
from: "vehicles",
localField: "vehicles",
foreignField: "_id",
as: "vehicle_doc"
} }
])
但是它给了我两个相同报价的文件,每个文件都包含一个车辆。我如何合并两个文件,只在一个引号中有车辆阵列。
我检查了提到的问题,与我的问题的区别在于我需要保留左侧集合中的其余文档信息, 解决方案中的想法假设除了_id之外没有其他项目可以投影,但我还需要传回其余的引用信息。