通过猫鼬使用.where()与.update()查询? [英] use .where() Query with .update() via mongoose?

查看:77
本文介绍了通过猫鼬使用.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对象,该对象具有您要询问的方法:updatefindOneAndUpdate

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) {});

这篇关于通过猫鼬使用.where()与.update()查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆