【发布时间】:2016-02-25 21:22:28
【问题描述】:
我尝试使用 mongoose (3.8.37) 更新多个文档,但没有更新任何文档。
我已经完成了从其他问题中学到的所有事情(见下文):
- 使用回调函数
- 指定multi:true
我的更新声明:
Animal.where({ type: type}).update({deleted:1}, { multi: true, overwrite: true }, function (err,doc) {
console.log("updates: "+doc);
});
更新:0
当我只计算文件时,我会得到一个结果。 => 查询正确
Animal.where({type: type}).count(function (err,doc) {
console.log("count: "+doc);
});
计数:299
当我省略 multi:true 选项时,第一条记录被更新。 => 更新语句也是正确的
Animal.where({ type: type}).update({deleted:-1}, function (err,doc) {
console.log("updates: "+doc);
});
更新:1
那么错误在哪里?
关于这个话题有几个问题。不幸的是,这些都不能解决我的问题。
- how can i update multiple documents in mongoose
- Mongoose update multiple documents doesn't update anything
- Mongoose: how to update *all* persons matching a condition?
** 更新
我added a log callback 发现只要指定了选项 (multi:true),就不会执行对 mongodb 的查询。
【问题讨论】:
-
尝试将您的
.update({deleted:1},...更改为.update({ $set: { deleted:1}},{multi: true}, function... -
@Molda 感谢您的提示。但我已经尝试过了。没有帮助。