猫鼬:如何更新所有符合条件的人? [英] Mongoose: how to update *all* persons matching a condition?
问题描述
我已经做过这样的事情(请原谅我对示例的愚蠢...:-):
I have do do something like this (please forgive me about the stupidity of the example... :-):
var dateRef = new Date(1990, 1, 1);
Person
.where('dateOfBirth').gte(dateRef)
.update({ $set: { isYoung: true } }, function (err, count) {
})
;
即:对于每个出生日期(dateOfBirth
)晚于参考日期的Person文档,我想将其设置为true
字段isYoung
.
I.e.: I'd like to $set to true
field isYoung
for every Person document whose date of birth (dateOfBirth
) is later than a reference date.
即使(当前)有很多文档符合条件(所有年轻人,这里... :-),我的代码也只适用于一个人.
My code is working for just one person, even if (currently) many documents do match the condition (all young people, here... :-).
有任何线索吗?
P.S .:作为奖励,如果可能的话,我也想将不匹配的人isYoung
字段设置为false
...
P.S.: as a bonus, I'd like to set not-matching persons isYoung
field to false
, too, if possible...
推荐答案
您应该在更新中使用multi:true
来更新多个文档,例如:
You should use multi:true
in update to update multiple documents like :
var dateRef = new Date(1990, 1, 1);
Person
.where('dateOfBirth').gte(dateRef)
.update({ $set: { isYoung: true } },{multi: true}, function (err, count) {
});
这篇关于猫鼬:如何更新所有符合条件的人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!