Mongoose .update方法为什么只能修改一个记录
发布于 7 年前 作者 hfqf 10583 次浏览 来自 问答

Mongoose .update方法为什么只能修改一个记录,不应该这样的啊。 屏幕快照 2017-07-21 下午2.39.04.png 屏幕快照 2017-07-21 下午2.39.24.png 屏幕快照 2017-07-21 下午2.39.28.png 哪里出问题了?

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)

文档是个好东西

http://mongoosejs.com/docs/api.html#model_Model.update

没记错的话应该有个 multi 的选项,设为 true 就可以了

回到顶部