【问题标题】:use .where() Query with .update() via mongoose?通过猫鼬使用 .where() 查询和 .update()?
【发布时间】:2013-09-11 11:00:03
【问题描述】:

如何在猫鼬中使用.where().update()?查询条件是传递给 .update() 的第一个参数。我是否应该将查询条件的null 传递给.update() 并在其后面加上.where('_id', id)?另外,我也可以将它应用到.findByIdAndUpdate()/.findOneAndUpdate() 吗?

如果没有,是否有 Query 方法,我可以在其中 .update() 仅使用更新文档并通过链中的其他 .where() 方法指定条件?

【问题讨论】:

  • 你能更明确地解释你的意图是什么吗?从您的数据库中,您尝试检索哪些文件,以及您尝试更新哪些文件?

标签: node.js mongodb mongoose


【解决方案1】:

在 Model 类上调用 where 会返回一个 Query 对象,该对象具有您所询问的那种方法:updatefindOneAndUpdate

因此,您可以将它们链接在一起以执行以下操作:

MyModel.where('_id', id).update({$set: {foo: 'bar'}}, function (err, count) {});

【讨论】:

猜你喜欢
  • 2016-02-21
  • 2014-10-22
  • 2014-10-19
  • 1970-01-01
  • 2013-05-06
  • 2012-02-03
  • 1970-01-01
  • 2013-07-07
  • 2015-10-26
相关资源
最近更新 更多