Wednesday, December 21, 2016

How to write query in mongodb with "like" ?


I want query something as SQL's like query:


select * from users where name like '%a%';

How to write the same query in mongodb :

This will match all the value that contain "name" a.

db.users.find({name: /a/})  //like '%a%'
This will match all the value that contain "name" starting with pa.

db.users.find({name: /^pa/}) //like 'pa%' 
This will match all the value that contain "name" end with pa.

db.users.find({name: /ro$/}) //like '%ro'

If you like this post don't forgot to leave a comment..

Chears :)
Happy Coding
Related Posts Plugin for WordPress, Blogger...