Mongoose .update方法为什么只能修改一个记录
Mongoose .update方法为什么只能修改一个记录,不应该这样的啊。 哪里出问题了?
6 回复
头已经大了
好了,原来是需要multi : true。
Model.update(conditions, doc, [options], [callback])
Examples:
MyModel.update({ age: { $gt: 18 } }, { oldEnough: true }, fn);
MyModel.update({ name: 'Tobi' }, { ferret: true }, { multi: true }, function (err, raw) {
if (err) return handleError(err);
console.log('The raw response from Mongo was ', raw);
});
Valid options:
- safe (boolean) safe mode (defaults to value set in schema (true))
- upsert (boolean) whether to create the doc if it doesn’t match (false)
- multi (boolean) whether multiple documents should be updated (false) runValidators: if true, runs update validators on this command. Update validators validate the update operation against the model’s schema.
- setDefaultsOnInsert: if this and upsert are true, mongoose will apply the defaults specified in the model’s schema if a new document is created. This option only works on MongoDB >= 2.4 because it relies on MongoDB’s $setOnInsert operator.
- strict (boolean) overrides the strict option for this update
- overwrite (boolean) disables update-only mode, allowing you to overwrite the doc (false)
文档是个好东西
没记错的话应该有个 multi 的选项,设为 true 就可以了
@KingTree 3q
@jkeylu 3q