我正在创建两个集合,一个是瞬间创建的,然后从主集合中“填充”到另一个文档中。
我很难理解如何以类似于下面的方式更新数组的特定索引。
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: location未定义“,"code“:139,"codeName”:"JSInterpreterFailure"}
人力资源收集文档如下所示:

在我试图添加的临时集合中的部门文档如下:

发布于 2017-02-22 01:12:49
Ahhhh...just看到了它,我以前有一个forEach,但是将它更改为索引数组:
这是:
db.tempDEPTS.find({"LOCATION_ID":location.LOCATION_ID}).forEach(function(dept)应该是这样:
db.tempDEPTS.find({"LOCATION_ID":country.location[i].LOCATION_ID}).forEach(function(dept)现在我的部门被塞进了基于location_id的位置文档中:

这肯定是一种更好的方法,但似乎奏效了。
https://stackoverflow.com/questions/42380824
复制相似问题