How to query MongoDB with “like”?

后端 未结 30 2068
予麋鹿
予麋鹿 2020-11-21 05:51

I want to query something with SQL\'s like query:

SELECT * FROM users  WHERE name LIKE \'%m%\'

How to do I achieve the same in

30条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 06:24

    In nodejs project and use mongoose use Like query

    var User = mongoose.model('User');
    
    var searchQuery={};
    searchQuery.email = req.query.email;
    searchQuery.name = {$regex: req.query.name, $options: 'i'};
    User.find(searchQuery, function(error, user) {
                    if(error || user === null) {
                        return res.status(500).send(error);
                    }
                    return res.status(200).send(user);
                });
    

提交回复
热议问题