Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to use a like query in mongo php

Tags:

regex

php

mongodb

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

<?php
$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??

like image 445
raagavan Avatar asked Feb 15 '11 21:02

raagavan


1 Answers

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);
like image 134
Justin Jenkins Avatar answered Sep 23 '22 19:09

Justin Jenkins