使用猫鼬更新多个文档失败 [英] Update of multiple documents with mongoose fails
问题描述
我尝试使用 mongoose (3.8.37) 更新多个文档,但没有更新任何文档.
我已经完成了从其他问题中学到的所有事情(见下文):
- 使用回调函数
- 指定 multi:true
我的更新声明:
Animal.where({ type: type}).update({deleted:1}, { multi: true, overwrite: true }, function (err,doc) {console.log("更新:"+doc);});
<块引用>
更新:0
当我只计算文件时,我会得到结果.=> 查询正确
Animal.where({type: type}).count(function (err,doc) {console.log("计数:"+doc);});
<块引用>
计数:299
当我省略 multi:true 选项时,会更新第一条记录.=> 更新语句也是正确的
Animal.where({ type: type}).update({deleted:-1}, function (err,doc) {console.log("更新:"+doc);});
<块引用>
更新:1
那么错误在哪里?
有几个问题涉及这个话题.不幸的是,这些都不能解决我的问题.
** 更新
我添加了日志回调并发现只要指定了选项(multi:true),就不会执行对 mongodb 的查询.
我已经设置了按预期工作的小例子,首先我调用 start()
来创建一些用户,然后 update()
var mongoose = require('mongoose');//v4.2.7var userSchema = mongoose.Schema({删除:数字,名称:字符串});var User = mongoose.model('user', userSchema);mongoose.connect('mongodb://127.0.0.1:27017/user');//开始();函数开始(){for (var i = 0; i <5; i++) {var 用户 = 新用户({已删除:1、姓名:我});用户保存();console.log('用户 ---> ', 用户);};User.find({}, function(err, docs){console.log('找到 ---> ', err, docs);});}更新();函数更新(){User.update({deleted:1}, {$set: {deleted: 0}}, {multi:true}, function(err, numAffected){console.log('更新 ---> ', err, numAffected);});}
我不确定为什么更新不适用于 where(...)
函数更新(){//这不起作用User.where({deleted:1}).update({$set: {deleted: 0}}, {multi:true}, function(err, numAffected){console.log('更新 ---> ', err, numAffected);});}
I try to update multiple documents with mongoose (3.8.37), but no document is updated.
I've done all things, that I've learned from other questions (see below):
- Use a callback function
- Specify multi:true
My update statement:
Animal.where({ type: type}).update({deleted:1}, { multi: true, overwrite: true }, function (err,doc) {
console.log("updates: "+doc);
});
updates: 0
When I just count the documents, I'll get a result. => The query is correct
Animal.where({type: type}).count(function (err,doc) {
console.log("count: "+doc);
});
count: 299
When I omit the multi:true option, the first record is updated. => The update statement is correct, too
Animal.where({ type: type}).update({deleted:-1}, function (err,doc) {
console.log("updates: "+doc);
});
updates: 1
So where's the error?
There are several questions dealing with this topic. Unfortunately none of these solves my problem.
- 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?
** UPDATE
I've added a log callback and discovered that no query to the mongodb is executed as long as the options (multi:true) are specified.
I have setup small example which works as expected, first i called start()
to create some users then update()
var mongoose = require('mongoose'); //v4.2.7
var userSchema = mongoose.Schema({
deleted: Number,
name: String
});
var User = mongoose.model('user', userSchema);
mongoose.connect('mongodb://127.0.0.1:27017/user');
//start();
function start(){
for (var i = 0; i < 5; i++) {
var user = new User({
deleted: 1,
name: i
});
user.save();
console.log('user ---> ', user);
};
User.find({}, function(err, docs){
console.log('found ---> ', err, docs);
});
}
update();
function update (){
User.update({deleted:1}, {$set: {deleted: 0}}, {multi:true}, function(err, numAffected){
console.log('updated ---> ', err, numAffected);
});
}
I'm not sure why update doesn't work with where(...)
function update (){
// this doesn't work
User.where({deleted:1}).update({$set: {deleted: 0}}, {multi:true}, function(err, numAffected){
console.log('updated ---> ', err, numAffected);
});
}
这篇关于使用猫鼬更新多个文档失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!