0
我試圖做兩個集的加入/合併,按堆棧溢出以下職位:How do I perform the SQL Join equivalent in MongoDB?基本加入MongoDB中
通過用戶:奧蘭多貝塞拉
據我所知,MongoDB是不 - 關係,因爲V3.2有一些但有限的能力來做到這一點。
你能讓我知道如果有可能做我在下面嘗試(例如,我在正確的道路上),或者如果它是不可能的..或另一種方法是最好的?
下面是我收集的:分配
{
"_id" : ObjectId("58a65c082c5fc49016c6a3cd"),
"data" : [
{
"version" : 0,
"jobTaskId" : {
"id" : 16089453
},
"endTime" : "2017-02-17T01:14:00.000+0000",
"minutes" : 210,
"trafficEmployeeId" : {
"id" : 3422
}
},
{
"version" : 1,
"jobTaskId" : {
"id" : 16089453
},
"endTime" : "2017-02-16T01:14:00.000+0000",
"minutes" : 400,
"trafficEmployeeId" : {
"id" : 3422
}
}
]
}
下面是我收集的:員工
{
"_id" : ObjectId("58a66cc0c76ed0f7e9f52d0e"),
"data" : [
{
"version" : 67,
"userName" : "Jimjeff2",
"employeeDetails" : {
"id" : 3422,
"version" : 135
},
"personalDetails" : {
"id" : 24487,
"version" : 32,
"firstName" : "Jim",
"lastName" : "Jeffrey"
}
},
{
"version" : 37,
"userName" : "sandyub2",
"employeeDetails" : {
"id" : 3562,
"version" : 15
},
"personalDetails" : {
"id" : 24487,
"version" : 32,
"firstName" : "Sandy",
"lastName" : "Mason"
}
}
]
}
所以,我想很多分配的陣列匹配集合使用trafficEmployeeId.id,使用employeeDetails.id
我使用這個功能:
db.allocations.find().forEach(
function (findAllocations) {
findAllocations.employees = db.employees.findOne({ "trafficEmployeeId.id": findAllocations.trafficEmployeeId.id });
findAllocations.allocations = db.allocations.find({ "employeeDetails.id": findAllocations.employeeDetails.id }).toArray();
db.allocationsReloaded.insert(findAllocations);
}
);
db.allocationsReloaded.find().pretty()
我得到返回結果:
TypeError: findAllocations.trafficEmployeeId is undefined :
@(shell):3:61
[email protected]/mongo/shell/query.js:477:1
@(shell):1:1