【发布时间】:2017-07-11 20:56:50
【问题描述】:
我正在创建两个集合,一个是临时创建的,然后从主集合“填充”到另一个文档中。
我很难理解如何以类似于下面的方式更新数组的特定索引。
function stuffDepartmentsIntoLocations() {
var ops = [];
db.HR.find().forEach(function(country)
{
for(var i=0;i < country.location.length; i++) {
db.tempDEPTS.find({"LOCATION_ID":location.LOCATION_ID}).forEach(function(dept)
{
ops.push(
{
"updateOne":
{
"filter": {"_id": country._id },
"update\": {"$push": {"location[i].department":dept}}
}
}
);
if ( ops.length == 1000 ) {
db.HR.bulkWrite(ops,{ "ordered": false });
ops = [];
}
}
);
}
}
);
if ( ops.length > 0 ) {
db.HR.bulkWrite(ops,{ "ordered": false });
}
return true;
}"
这似乎不起作用:
"update": {"$push": {"location[i].department":dept}}
错误如下:
{ "serverUsed" : "127.0.0.1:27017" , "retval" : true , "ok" : 1.0} { “serverUsed”:“127.0.0.1:27017”,“ok”:0.0,“errmsg”: “ReferenceError:未定义位置 :\nstuffDepartmentsIntoLocations/
人力资源收集文档如下所示:
我尝试添加的临时集合中的部门文档如下:
【问题讨论】:
标签: mongodb