How do I query MongoDB with `like`?


I want to return users whose names starts with E in MongoDB. Something like SELECT * FROM users WHERE name LIKE 'E%' in SQL. But I'm not sure how to do it MongoDB.

1 Answer

4 years ago by

MongoDB uses regular expressions for pattern matching. You can query like below to get users whose names start with E

db.users.find({name: /^E/})

If you want to make a case in-sensitive query then you can use the option i as shown below:

db.users.find({name: /^E/i})
4 years ago by Anusha