Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I want to query something with SQL's like the query:

SELECT * FROM users WHERE name LIKE '%m%'

How to do I achieve the same in MongoDB? I can't find an operator for like in the documentation.

2 Answers

0 votes
by (106k points)

You can use something like as follows:

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

or, similar:

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

0 votes
by (50.2k points)

A regular expression is an important way of specifying a pattern for a complex search. The MongoDB $regex operator provides such a way to search a string after matching a specified pattern. mongodb uses regular expressions which are more powerful than "LIKE" in SQL. With regular expressions, you can create any pattern that you imagine.

Related questions

+2 votes
1 answer
0 votes
0 answers
asked Jun 24, 2021 in SQL by Harsh (1.5k points)
0 votes
2 answers
asked Oct 16, 2019 in Web Technology by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...