如何查找一个或另一个字段与值匹配的文档? [英] How to find a document where either one or another field matches a value?

查看:45
本文介绍了如何查找一个或另一个字段与值匹配的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模特是

var userSchema = new Schema({
    first_name : String,
    last_name : String,
    ...
});

由此,我需要执行搜索过滤器选项.为此,我要传递带有值的name参数.

From this, I need to do search filter option. For that I am passing name params with value.

现在我要返回包含first_name == namelast_name == name

如何为此编写查询?

推荐答案

User.findOne({
    $or: [
        {first_name: name},
        {last_name: name},
    ],
}, function(err, user) {
    ...
})

如果您希望不止一个文档匹配名称等于first_name或last_name,请使用find.

Use find if you expect there to be more than one document matching name to be equal to first_name or last_name.

这篇关于如何查找一个或另一个字段与值匹配的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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