- 4 months ago 22
PHP Question

How to make "LIKE" query work in MONGODB using PHP?

I have a list of street names and I want to select all that start with "Al".
In my mysql I would do something like

SELECT * FROM streets WHERE "street_name" LIKE "Al%"

How about mongodb using php?


Use a regular expression:

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


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

Turning this into PHP:

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