我是mongodb的新手,所以因为mongodb不完整的文档而让我感到压力,因为我没有错误,所以我的所有尝试都无法正常工作,让我对发生的事情和调试内容感到困惑......
我只需更新符合特定条件的数据库上的多条记录,并为非现有记录创建新条目.我相信我可以使用update,upsert和multi进行单一数据库访问.这是我想出的:
dbschema.Person.update( { person_id: { $in: ["734533604" ,"701084015"] } }, { $set: {"scores": 1200} }, { options: { upsert: true, multi: true } } );
Run Code Online (Sandbox Code Playgroud)
我也尝试了多种组合甚至旧版本,例如:
dbschema.Person.update( { person_id: { $in: ["734533604" ,"701084015"] } }, { $set: {"scores": 1200} }, { upsert: true }, { multi: true } );
Run Code Online (Sandbox Code Playgroud)
它们都不起作用......
请帮助我这个如此微不足道的东西......我可以轻松地在sql中做到这一点,但是nosql thingy是如此限制我..谢谢!
编辑:
find上的相同查询完美无缺:
dbschema.Person.find( { person_id: { $in: ["734533604" ,"701084015"] } }, function ( err, results ) {
console.log( 'result: ' + results );
console.log( 'error: ' + …Run Code Online (Sandbox Code Playgroud) 我努力更新同一集合的多个文档,这些文档需要将字段添加到 JSON“根”,以及一个名为logs的现有数组的新对象。
我正在尝试这个
db.getCollection('charges').
update({'supports.dest':ObjectId("xyz"),
date:{
$gte: new Date(2017, 11),
$lt: new Date(2017, 12)
},
"status": {$nin : ["Captured"]
}
},
{$set: {"status": "BillingSuspended",
"replacedStatus":"Captured"}
},
{multi:true},
{$push :
{logs : {"replacedStatus" : "Captured" ,
date: new Date ('2017-12-13T22:00:00.000Z')
}
}
}
)
Run Code Online (Sandbox Code Playgroud)
我在下面收到这个错误,我尝试取出 multi:true 但后来我失去了允许我同时更新许多文档的 multi 属性。我想要一个在 Robomongo 上运行的查询。如果你能帮助我,我将不胜感激。
错误:
[Failed to execute script.
Error: Fourth argument must be empty when specifying upsert and multi with an object. :
DBCollection.prototype._parseUpdate@src/mongo/shell/collection.js:522:1
DBCollection.prototype.update@src/mongo/shell/collection.js:552:18
@(shell):1:1]
Run Code Online (Sandbox Code Playgroud)