Node.js MongoDB - 如何使用 Mongoose 更新多个文档?
Node.js MongoDB - How to update multiple documents with Mongoose?
我有一个 users
collection 并且每个 user
都有很多联系人。当 user
删除他们的帐户时,我希望从与该用户有联系的所有用户的联系人数组中删除该用户的 ID。我试过这个 Model.Update
查询,但它不起作用。到目前为止,这是我的代码:
User.update({'userId':{ $in: userIds },
$pullAll: {'contacts': [myId] },'multi': true
},function(err, count) {
if(err){
console.log(err);
}else{
console.log(count);
}
});
更新文档和 multi
应作为单独的参数传递:
User.update({
userId : { $in : userIds } // conditions
}, {
$pullAll : { contacts : [myId] } // document
}, {
multi : true // options
}, function(err, count) {
if (err) {
console.log(err);
} else {
console.log(count);
}
});
文档 here.
可以用多个条件更新多个文档
Model.update({
_id : { $in : ids} // conditions
}, {
$set: {deletion_indicator: constants.N} // document
}, {
multi : true // options
}, function(err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
}
});
我有一个 users
collection 并且每个 user
都有很多联系人。当 user
删除他们的帐户时,我希望从与该用户有联系的所有用户的联系人数组中删除该用户的 ID。我试过这个 Model.Update
查询,但它不起作用。到目前为止,这是我的代码:
User.update({'userId':{ $in: userIds },
$pullAll: {'contacts': [myId] },'multi': true
},function(err, count) {
if(err){
console.log(err);
}else{
console.log(count);
}
});
更新文档和 multi
应作为单独的参数传递:
User.update({
userId : { $in : userIds } // conditions
}, {
$pullAll : { contacts : [myId] } // document
}, {
multi : true // options
}, function(err, count) {
if (err) {
console.log(err);
} else {
console.log(count);
}
});
文档 here.
可以用多个条件更新多个文档
Model.update({
_id : { $in : ids} // conditions
}, {
$set: {deletion_indicator: constants.N} // document
}, {
multi : true // options
}, function(err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
}
});