MongoDB: Is it possible to make a case-insensitive query?

后端 未结 24 1859
谎友^
谎友^ 2020-11-22 04:44

Example:

> db.stuff.save({\"foo\":\"bar\"});

> db.stuff.find({\"foo\":\"bar\"}).count();
1
> db.stuff.find({\"foo\":\"BAR\"}).count();
0

相关标签:
24条回答
  • 2020-11-22 05:18

    Keep in mind that the previous example:

    db.stuff.find( { foo: /bar/i } );
    

    will cause every entries containing bar to match the query ( bar1, barxyz, openbar ), it could be very dangerous for a username search on a auth function ...

    You may need to make it match only the search term by using the appropriate regexp syntax as:

    db.stuff.find( { foo: /^bar$/i } );
    

    See http://www.regular-expressions.info/ for syntax help on regular expressions

    0 讨论(0)
  • 2020-11-22 05:19

    I'm surprised nobody has warned about the risk of regex injection by using /^bar$/i if bar is a password or an account id search. (I.e. bar => .*@myhackeddomain.com e.g., so here comes my bet: use \Q \E regex special chars! provided in PERL

    db.stuff.find( { foo: /^\Qbar\E$/i } );
    

    You should escape bar variable \ chars with \\ to avoid \E exploit again when e.g. bar = '\E.*@myhackeddomain.com\Q'

    Another option is to use a regex escape char strategy like the one described here Javascript equivalent of Perl's \Q ... \E or quotemeta()

    0 讨论(0)
  • 2020-11-22 05:20

    Using Mongoose this worked for me:

    var find = function(username, next){
        User.find({'username': {$regex: new RegExp('^' + username, 'i')}}, function(err, res){
            if(err) throw err;
            next(null, res);
        });
    }
    
    0 讨论(0)
  • 2020-11-22 05:21

    If you need to create the regexp from a variable, this is a much better way to do it: https://stackoverflow.com/a/10728069/309514

    You can then do something like:

    var string = "SomeStringToFind";
    var regex = new RegExp(["^", string, "$"].join(""), "i");
    // Creates a regex of: /^SomeStringToFind$/i
    db.stuff.find( { foo: regex } );
    

    This has the benefit be being more programmatic or you can get a performance boost by compiling it ahead of time if you're reusing it a lot.

    0 讨论(0)
  • 2020-11-22 05:21

    TL;DR

    Correct way to do this in mongo

    Do not Use RegExp

    Go natural And use mongodb's inbuilt indexing , search

    Step 1 :

    db.articles.insert(
       [
         { _id: 1, subject: "coffee", author: "xyz", views: 50 },
         { _id: 2, subject: "Coffee Shopping", author: "efg", views: 5 },
         { _id: 3, subject: "Baking a cake", author: "abc", views: 90  },
         { _id: 4, subject: "baking", author: "xyz", views: 100 },
         { _id: 5, subject: "Café Con Leche", author: "abc", views: 200 },
         { _id: 6, subject: "Сырники", author: "jkl", views: 80 },
         { _id: 7, subject: "coffee and cream", author: "efg", views: 10 },
         { _id: 8, subject: "Cafe con Leche", author: "xyz", views: 10 }
       ]
    )
     
    

    Step 2 :

    Need to create index on whichever TEXT field you want to search , without indexing query will be extremely slow

    db.articles.createIndex( { subject: "text" } )
    

    step 3 :

    db.articles.find( { $text: { $search: "coffee",$caseSensitive :true } } )  //FOR SENSITIVITY
    db.articles.find( { $text: { $search: "coffee",$caseSensitive :false } } ) //FOR INSENSITIVITY
    
    
     
    
    0 讨论(0)
  • 2020-11-22 05:23

    Use RegExp, In case if any other options do not work for you, RegExp is a good option. It makes the string case insensitive.

    var username = new RegExp("^" + "John" + "$", "i");;
    

    use username in queries, and then its done.

    I hope it will work for you too. All the Best.

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