How do I make case-insensitive queries on Mongodb?

后端 未结 12 1196
北恋
北恋 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:18

    I just solved this problem a few hours ago.

    var thename = 'Andrew'
    db.collection.find({ $text: { $search: thename } });
    
    • Case sensitivity and diacritic sensitivity are set to false by default when doing queries this way.

    You can even expand upon this by selecting on the fields you need from Andrew's user object by doing it this way:

    db.collection.find({ $text: { $search: thename } }).select('age height weight');
    

    Reference: https://docs.mongodb.org/manual/reference/operator/query/text/#text

提交回复
热议问题