How to make "LIKE" query work in MongoDB? How to make "LIKE" query work in MongoDB? mongodb mongodb

How to make "LIKE" query work in MongoDB?


Use a regular expression:

db.streets.find( { street_name : /^Al/i } );

or:

db.streets.find( { street_name : { $regex : '^Al', $options: 'i' } } );

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions

Turning this into PHP:

$regex = new MongoRegex("/^Al/i");$collection->find(array('street_name' => $regex));


See: http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

Also, highly recommend just using the native mongodb connector from PHP instead of a wrapper. It's way faster than any wrapper.

http://php.net/class.mongodb


here is my working example:

<?phpuse MongoDB\BSON\Regex;$collection = $yourMongoClient->yourDatabase->yourCollection;$regex = new Regex($text, 's');$where = ['your_field_for_search' => $regex];$cursor = $collection->find($where);//Lets iterate through collection