I have saved regular expressions as a value in collection. I want to check a string against that regular expression in mongodb, whether it Is matching or not? I got to know that I should use javascript code for the same but it’s not working from php-mongodb.
It’s working from mongodb client console.
Here My code,
<?php
$dbName = "XYZ";
try
{
$dbInstanceName = $dbName;
$mongoInstance = new Mongo("localhost:27017", array("persist" => "x"));
$dbReference = $mongoInstance->$dbInstanceName;
$db = $mongoInstance->$dbInstanceName;
}
catch (Exception $e)
{
echo "<pre>";
print_r($e);
echo "</pre>";
exit;
}
$query = new MongoCode("function (){return RegExp(this.regCountry).test('India');}");
$c = "Countries1";
$cursor = $db->$c->find($query);
if ($cursor->count() == 0)
{
echo "<pre>";
print_r("No Data Found.!");
echo "</pre>";
exit;
}
else
{
$data = array();
while ($cursor->hasNext())
{
$cursor->next();
array_push($data, $cursor->current());
}
}
echo "<pre>";
print_r($data);
echo "</pre>";
exit;
?>
its working but return nothing….!
resonse :
Array
(
)
here, consoled screen sort, its given return a value.

here my data sample :

What’s wrong?
Finally I got Answer :
change my code shown in below
output :