How to query MongoDB with “like”?

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

提交回复
热议问题