How do I make case-insensitive queries on Mongodb?

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

    To find case-insensitive literals string:

    Using regex (recommended)

    db.collection.find({
        name: {
            $regex: new RegExp('^' + name.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '$', 'i')
        }
    });
    

    Using lower-case index (faster)

    db.collection.find({
        name_lower: name.toLowerCase()
    });
    

    Regular expressions are slower than literal string matching. However, an additional lowercase field will increase your code complexity. When in doubt, use regular expressions. I would suggest to only use an explicitly lower-case field if it can replace your field, that is, you don't care about the case in the first place.

    Note that you will need to escape the name prior to regex. If you want user-input wildcards, prefer appending .replace(/%/g, '.*') after escaping so that you can match "a%" to find all names starting with 'a'.

    0 讨论(0)
  • 2020-11-27 12:14

    To find case Insensitive string use this,

    var thename = "Andrew";
    db.collection.find({"name":/^thename$/i})
    
    0 讨论(0)
  • 2020-11-27 12:14

    The following query will find the documents with required string insensitively and with global occurrence also

    db.collection.find({name:{
                                 $regex: new RegExp(thename, "ig")
                             }
                        },function(err, doc) {
                                             //Your code here...
                      });
    
    0 讨论(0)
  • 2020-11-27 12:16

    You'd need to use a case-insensitive regular expression for this one, e.g.

    db.collection.find( { "name" : { $regex : /Andrew/i } } );
    

    To use the regex pattern from your thename variable, construct a new RegExp object:

    var thename = "Andrew";
    db.collection.find( { "name" : { $regex : new RegExp(thename, "i") } } );
    

    Update: For exact match, you should use the regex "name": /^Andrew$/i. Thanks to Yannick L.

    0 讨论(0)
  • 2020-11-27 12:18

    MongoDB 3.4 now includes the ability to make a true case-insensitive index, which will dramtically increase the speed of case insensitive lookups on large datasets. It is made by specifying a collation with a strength of 2.

    Probably the easiest way to do it is to set a collation on the database. Then all queries inherit that collation and will use it:

    db.createCollection("cities", { collation: { locale: 'en_US', strength: 2 } } )
    db.names.createIndex( { city: 1 } ) // inherits the default collation
    

    You can also do it like this:

    db.myCollection.createIndex({city: 1}, {collation: {locale: "en", strength: 2}});
    

    And use it like this:

    db.myCollection.find({city: "new york"}).collation({locale: "en", strength: 2});
    

    This will return cities named "new york", "New York", "New york", etc.

    For more info: https://jira.mongodb.org/browse/SERVER-90

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题