How do I make case-insensitive queries on Mongodb?

后端 未结 12 1168
北恋
北恋 2020-11-27 12:02
var thename = \'Andrew\';
db.collection.find({\'name\':thename});

How do I query case insensitive? I want to find result even if \"andrew\";

12条回答
  •  独厮守ぢ
    2020-11-27 12:37

    I have solved it like this.

     var thename = 'Andrew';
     db.collection.find({'name': {'$regex': thename,$options:'i'}});
    

    If you want to query on 'case-insensitive exact matchcing' then you can go like this.

    var thename =  '^Andrew$';
    db.collection.find({'name': {'$regex': thename,$options:'i'}});
    

提交回复
热议问题