Mongoose Like Operator

时间:2018-04-11 04:02:52

标签: mongodb mongoose

下面的代码不起作用并返回一个空数组。有什么问题:

const user = mongoose.model('user', {
    firstName: String
});

async function usersBySearch(search) {
    return await user.find({firstName: '/' + search + "/"});
};

1 个答案:

答案 0 :(得分:3)

您应该使用$regex

user.find( { 'firstName' : { '$regex' : search, '$options' : 'i' } } )

where options:我将进行不区分大小写的匹配

https://docs.mongodb.com/manual/reference/operator/query/regex/