如何在猫鼬中根据和条件找到模型的不同字段? [英] how to find distinct field of a model upon an and condition in mongoose?

查看:34
本文介绍了如何在猫鼬中根据和条件找到模型的不同字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找满足这两个条件的模型的不同字段的列表.

I am looking to find list of the distinct fields of a model which satisfies both the two conditions.

来自文档:http://mongoosejs.com/docs/api.html#model_Model.独特的

我发现我可以在 Model.distinct() 中将条件数组作为第二个参数传递.但是条件数组似乎是通过或"操作链接起来的.也就是说,如果任何一个条件满足该字段就会被列出.

i found that i can pass an array of conditions as second parameter in Model.distinct(). But the array of conditions seems to get linked via an 'or' operation. That is if any one condition satisfies the field is listed.

但我只想在两个条件都满足的情况下获得该字段.

But i want to get the field only if both conditions are satisfies.

当我使用:

Model.distinct(field).and(arrayOfConditions).exec(callback);

然后我得到错误:

TypeError: Cannot call method 'map' of undefined
    at SchemaBoolean.handleArray (/Users/user1/node_modules/mongoose/lib/schema/boolean.js:56:14)

推荐答案

distinct 不返回完整功能的 Query 对象,因此您可以这样做:

distinct doesn't return a full functioning Query object so you would do it like this instead:

Model.distinct(field, {$and: arrayOfConditions}).exec(callback);

这篇关于如何在猫鼬中根据和条件找到模型的不同字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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