How to query MongoDB with “like”?

后端 未结 30 2041
予麋鹿
予麋鹿 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:09

    With MongoDB Compass, you need to use the strict mode syntax, as such:

    { "text": { "$regex": "^Foo.*", "$options": "i" } }
    

    (In MongoDB Compass, it's important that you use " instead of ')

    0 讨论(0)
  • 2020-11-21 06:09

    It seems that there are reasons for using both the javascript /regex_pattern/ pattern as well as the mongo {'$regex': 'regex_pattern'} pattern. See: MongoBD RegEx Syntax Restrictions

    This is not a complete RegEx tutorial, but I was inspired to run these tests after seeing a highly voted ambiguous post above.

    > ['abbbb','bbabb','bbbba'].forEach(function(v){db.test_collection.insert({val: v})})
    
    > db.test_collection.find({val: /a/})
    { "val" : "abbbb" }
    { "val" : "bbabb" }
    { "val" : "bbbba" }
    
    > db.test_collection.find({val: /.*a.*/})
    { "val" : "abbbb" }
    { "val" : "bbabb" }
    { "val" : "bbbba" }
    
    > db.test_collection.find({val: /.+a.+/})
    { "val" : "bbabb" }
    
    > db.test_collection.find({val: /^a/})
    { "val" : "abbbb" }
    
    > db.test_collection.find({val: /a$/})
    { "val" : "bbbba" }
    
    > db.test_collection.find({val: {'$regex': 'a$'}})
    { "val" : "bbbba" }
    
    0 讨论(0)
  • 2020-11-21 06:09

    As Mongo shell support regex, that's completely possible.

    db.users.findOne({"name" : /.*sometext.*/});
    

    If we want the query to be case-insensitive, we can use "i" option, like shown below:

    db.users.findOne({"name" : /.*sometext.*/i});
    
    0 讨论(0)
  • 2020-11-21 06:11

    You have 2 choices:

    db.users.find({"name": /string/})
    

    or

    db.users.find({"name": {"$regex": "string", "$options": "i"}})
    

    On second one you have more options, like "i" in options to find using case insensitive. And about the "string", you can use like ".string." (%string%), or "string.*" (string%) and ".*string) (%string) for example. You can use regular expression as you want.

    Enjoy!

    0 讨论(0)
  • 2020-11-21 06:12

    For PHP mongo Like.
    I had several issues with php mongo like. i found that concatenating the regex params helps in some situations PHP mongo find field starts with. I figured I would post on here to contribute to the more popular thread

    e.g

    db()->users->insert(['name' => 'john']);
    db()->users->insert(['name' => 'joe']);
    db()->users->insert(['name' => 'jason']);
    
    // starts with
    $like_var = 'jo';
    $prefix = '/^';
    $suffix = '/';
    $name = $prefix . $like_var . $suffix;
    db()->users->find(['name' => array('$regex'=>new MongoRegex($name))]);
    output: (joe, john)
    
    // contains
    $like_var = 'j';
    $prefix = '/';
    $suffix = '/';
    $name = $prefix . $like_var . $suffix;
    db()->users->find(['name' => array('$regex'=>new MongoRegex($name))]);
    
    output: (joe, john, jason)
    
    0 讨论(0)
  • 2020-11-21 06:13
    db.users.insert({name: 'paulo'})
    db.users.insert({name: 'patric'})
    db.users.insert({name: 'pedro'})
    
    db.users.find({name: /a/})  //like '%a%'
    

    out: paulo, patric

    db.users.find({name: /^pa/}) //like 'pa%' 
    

    out: paulo, patric

    db.users.find({name: /ro$/}) //like '%ro'
    

    out: pedro

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