mongoose 多条件 模糊查询

版权声明:转载请评论留言 https://blog.csdn.net/solocao/article/details/82944535

需要知道两个标识符就可以搞定。
$or 用于多条件查询 http://www.nodeclass.com/api/mongoose.html#query_Query-or
$regex 用于模糊查询 http://www.nodeclass.com/api/mongoose.html#query_Query-regex

试例代码:

//从URL中传来的 keyword参数
const keyword = this.params.keyword 
//不区分大小写
const reg = new RegExp(keyword, 'i') 
const result = await User.find(
    {
        $or : [ //多条件,数组
            {nick : {$regex : reg}},
            {email : {$regex : reg}}
        ]
    },
    {
        password : 0 // 返回结果不包含密码字段
    },
    {
        sort : { _id : -1 },// 按照 _id倒序排列
        limit : 100 // 查询100条
    }
)

猜你喜欢

转载自blog.csdn.net/solocao/article/details/82944535