How to query MongoDB with “like”?

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

    Regex are expensive are process.

    Another way is to create an index of text and then search it using $search.

    Create a text Index of fields you want to make searchable:

    db.collection.createIndex({name: 'text', otherField: 'text'});
    

    Search for a string in text index:

    db.collection.find({
      '$text'=>{'$search': "The string"}
    })
    
    0 讨论(0)
  • 2020-11-21 06:26

    In SQL, the ‘like’ query is looks like this :

    select * from users where name like '%m%'
    

    In MongoDB console, it looks like this :

    db.users.find({"name": /m/})     // Not JSON formatted
    
    db.users.find({"name": /m/}).pretty()  // JSON formatted
    

    In addion pretty() method will in all the places where produce formatted JSON structure which is more readable.

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

    Already u got the answers but to match regex with case insensitivity

    You could use the following query

    db.users.find ({ "name" : /m/i } ).pretty()
    

    The i in the /m/i indicates case insensitivity and .pretty() provides a more pretty output

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

    String deepakparmar, dipak, parmar

    db.getCollection('yourdb').find({"name":/^dee/})
    

    ans deepakparmar

    db.getCollection('yourdb').find({"name":/d/})
    

    ans deepakparmar, dipak

    db.getCollection('yourdb').find({"name":/mar$/})
    

    ans deepakparmar, parmar

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

    There are already many answers. I am giving different types of requirements and solutions for string search with regex.

    You can do with regex which contain word i.e like. Also you can use $options => i for case insensitive search

    Contains string

    db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
    

    Doesn't Contains string only with regex

    db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
    

    Exact case insensitive string

    db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
    

    Start with string

    db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
    

    End with string

    db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
    

    Keep this as a bookmark, and a reference for any other alterations you may need.

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

    Use aggregation substring search (with index!!!):

    db.collection.aggregate([{
            $project : {
                fieldExists : {
                    $indexOfBytes : ['$field', 'string']
                }
            }
        }, {
            $match : {
                fieldExists : {
                    $gt : -1
                }
            }
        }, {
            $limit : 5
        }
    ]);
    
    0 讨论(0)
提交回复
热议问题