How to query MongoDB with “like”?

后端 未结 30 2677
予麋鹿
予麋鹿 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"}
    })
    

提交回复
热议问题