MongoDB 中更新集合时出错

Error on updating collection in MongoDB

我在 MongoDB 中有一个数据库,其中包含两个集合 x 和 y。我正在尝试更新字段 x.Venue = y.name,其中 x.club = y.team。 我在 mongoDB.

中尝试了以下代码
db.x.find().forEach(function(myDoc) {
       var temp = db.y.findOne({"team" : myDoc.club});
       if (temp) {
             myDoc.Venue = temp.name;
             db.x.save(myDoc);
        }
 });

现在,如果我错了请纠正我,myDoc varibale 是 forEach 的游标 function.i 已验证临时数据正确并且 myDoc.Venue 已正确分配。虽然,我收到以下错误。

2016-07-01T23:43:58.382+0530 E QUERY    [thread1] Error: error: {
    "waitedMS" : NumberLong(0),
    "ok" : 0,
    "errmsg" : "cannot compare to undefined",
    "code" : 2
} :

_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCommandCursor@src/mongo/shell/query.js:689:1
DBQuery.prototype._exec@src/mongo/shell/query.js:118:28
DBQuery.prototype.hasNext@src/mongo/shell/query.js:276:5
DBCollection.prototype.findOne@src/mongo/shell/collection.js:289:10
@(shell):1:61
DBQuery.prototype.forEach@src/mongo/shell/query.js:488:1
@(shell):1:1

谁能帮我解决这个错误。

我遇到了相同类型的问题并已解决。当您想比较 db property/fieldundefined 时,会发生此错误。所以你应该先检查再比较。

在您的查询中比较 {"team" : myDoc.club},其中 myDoc.clubundefined,所以发生了这个错误。

所以可以试试这个

db.x.find().forEach(function(myDoc) {
       if(myDoc.club){
           var temp = db.y.findOne({"team" : myDoc.club});
           if (temp) {
              myDoc.Venue = temp.name;
              db.x.save(myDoc);
           }
       }
 });