我有两个包含以下内容的集合:
"DeviceId" : "",
"FirstName" : "",
"LastName" : "",
"AllowDomains" : "",
"JobLocationName" : ""
等等。我编写此查询首先检查collection1中的deviceId
是否与collection2中的deviceId
相同。如果相同则不做任何事情,否则必须复制到temp集合中。
但首先:
它根本没有比较,这些代码复制所有文件?
第二,复制过程中间我收到了错误?
function compareCollection (dv, F2){
var compare = F2.find().forEach(function(doc1){
if (doc1.DeviceId === dv ) {
return false;
} else {
insertToColl(doc1.DeviceId)
}
});
}
var result;
db.getCollection('FinalLocation').find({}, {DeviceId: 1}).forEach(function(u)
{
result = u.DeviceId;
});
compareCollection(result , db.getCollection('FinalLocation_'));
function insertToColl(info){
var post = {
"DeviceId" : info,
"FirstName" : "",
"LastName" : "",
"AllowDomains" : "",
"JobLocationName" : ""
};
db.getCollection('tempColl').save([post], function (err, result) {
if (err) {
console.log("ERROR ", err);
}
else {
console.log("SUCCESS INSERTED in to users collection _is are ", result.length, result)
}
});
}
当mongo想要将数据插入集合时会出现这些错误:
2017-09-26T15:41:47.467+0330 E QUERY [thread1] Error: can't save a number or string :
DBCollection.prototype.save@src/mongo/shell/collection.js:615:1
insertToColl@(shell):2:3
compareCollection/compare<@(shell):7:7
DBQuery.prototype.forEach@src/mongo/shell/query.js:501:1
compareCollection@(shell):2:17
@(shell):1:1