我遇到了在mongoDB中更新文档的问题。我需要的是:遍历集合中的每个文档,修复数据中的一些问题并保存文档:
var isDateValueAsObject = function(date){
return (date && date.value && isObject(date.value));
};
var checkPropAndUpdate = function(obj, prop){
if(isDateValueAsObject(obj[prop])){
if(obj[prop].value.value){
obj[prop].value = obj[prop].value.value;
return true;
}
}
return false;
};
db.getCollection('trials').find({}).forEach(function(doc){
if(doc.revisions && doc.revisions.length > 0){
var updated = false;
for(var i = 0; i < doc.revisions.length; i++){
var rev = doc.revisions[i];
updated = updated || checkPropAndUpdate(rev, "lastchangedDate");
updated = updated || checkPropAndUpdate(rev, "startDate");
updated = updated || checkPropAndUpdate(rev, "completionDate");
updated = updated || checkPropAndUpdate(rev, "firstreceivedDate");
updated = updated || checkPropAndUpdate(rev, "primaryCompletionDate");
updated = updated || checkPropAndUpdate(rev, "verificationDate");
}
if(updated){
db.getCollection('trials').save(doc);
print("updating: "+doc.trialId);
}
}
});
由于某种原因db.getCollection(&#39;试验&#39;)。save(doc);什么也没做。我做错了什么?