我努力让同一个集合的多个文档更新,需要将字段添加到JSON“root”,以及一个名为 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')
}
}
}
)
我正在收到这个错误,我尝试取出多个:true然后我松开了允许我同时更新许多文档的多属性。我想要一个在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]
答案 0 :(得分:1)
你必须尝试这样
db.getCollection('charges').updateMany(
{
'supports.dest': ObjectId("xyz"),
'date': {
'$gte': new Date(2017, 11),
'$lt': new Date(2017, 12)
},
"status": {
$nin: ["Captured"]
}
},
{
$set: {
"status": "BillingSuspended",
"replacedStatus": "Captured"
},
$push: {
logs: {
"replacedStatus": "Captured",
date: new Date('2017-12-13T22:00:00.000Z')
}
}
})