How to query MongoDB with “like”?

481    Asked by AugustinaFuentes in Salesforce , Asked on Jul 15, 2021

 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.

Answered by Asutosh Jha

You can use something like as follows:

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

Note: You can use a SQL LIKE statement in MongoDB with the find command and search for word similarities using regex on your MongoDB database.



Your Answer

Interviews

Parent Categories