mamdouh asked this 7 years ago

MongoDB how to search for a string or pattern in a field?

How do I search for a string inside a field in MongoDB? In SQL if i want to search name column for a string pizza

SELECT * FROM restaurants WHERE name LIKE '%pizza%';

What is the equivalent of this in MongoDB?


Best Answer by go_mongo 7 years ago

For a case-insensitive search,

db.restaurants.find( { name: /pizza/i } )
go_mongo 7 years ago
1 like

MongoDB equivalent would be

db.restaurants.find({"name": /pizza/})
Binoy 7 years ago

db.restaurants.find( { name: /pizza/ } )