raagavan raagavan - 2 years ago 57
PHP Question

I need to use a like query in mongo php

I have used this query in shell. It worked fine. If I use it in php, it shows me an error

$m = new Mongo();
$db = $m->foo;
$collection = $db->base;
$query = array( "ro" => '/^a/' );
$cursor = $collection->find($query);

foreach ($cursor as $obj) {
echo $obj["ro"] . "<br/>";

I am not sure, will
db.base.find({"ro": /^a/}).limit(10);
query work in php??

Answer Source

You need to use MongoRegex() ...

See docs here: http://php.net/manual/en/class.mongoregex.php

So the PHP code would be something like ...

$regex = new MongoRegex("/^a/"); 
$where = array('ro' => $regex);
$cursor = $collection->find($where);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download