通过猫鼬使用.where()与.update()查询?
如何在猫鼬中将.where()
与.update()
一起使用?查询条件是传递给.update()的第一个参数.我应该将查询条件的null
传递给.update()
并跟随一个.where('_id', id)
吗?另外,我也可以将其应用于.findByIdAndUpdate()
/.findOneAndUpdate()
吗?
How can I use .where()
with .update()
in mongoose? The query conditions are the first argument passed .update(). Should I pass null
for the query condition to .update()
and follow it with a .where('_id', id)
? Also, can I apply this to .findByIdAndUpdate()
/.findOneAndUpdate()
as well?
如果没有,是否有一个Query
方法,我可以只用更新文档来.update()
并通过链中的其他.where()
方法指定条件?
If not, is there a Query
method in which I can .update()
with just the update document and specify the conditions via other .where()
methods in the chain?
在Model类上调用where
将返回Query
对象,该对象具有您要询问的方法:update
,findOneAndUpdate
Calling where
on a Model class returns a Query
object which has the sort of methods you're asking about: update
, findOneAndUpdate
.
因此您可以将它们链接在一起以执行以下操作:
So you can chain them together to do things like:
MyModel.where('_id', id).update({$set: {foo: 'bar'}}, function (err, count) {});