我已经将正则表达式保存为集合中的值。我想在mongodb中根据正则表达式检查字符串,无论它是否匹配?我知道我应该使用javascript代码来做同样的事情,但是它在php-mongodb中不起作用。
它在mongodb客户端控制台上工作。
下面是我的代码,
<?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;?>
它在工作,但没有返回任何结果...!
共鸣:
Array
(
)这里,控制台屏幕排序,其给定的返回值。

下面是我的数据示例:

怎么了?
发布于 2012-07-25 15:22:38
最后我得到了答案:
更改我的代码,如下所示
<?php
$dbName = "Scrubly";
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 = "function(){return db.Countries1.find(function (){return RegExp(this.regCountry).test('India');}).toArray();}";
$result = $db->execute($query);
echo "<pre>";
print_r($result);
echo "</pre>";
exit;
?>输出:
Array
(
[retval] => Array
(
[0] => Array
(
[_id] => MongoId Object
(
[$id] => 500e5b456bcb44780d000072
)
[country] => India
[countryCode] => IN
[createdAt] => 2012-07-24 13:52:29
[id] => 386
[regCountry] => ^(.*)India(.*)$
[updatedAt] => 2012-07-24 13:52:29
)
)
[ok] => 1
)发布于 2012-07-24 21:40:41
如果我上面的评论不起作用,请尝试更改以下内容:
$cursor = $db->$c->find($query);至:
$cursor = $db->selectCollection($c)->find($query);https://stackoverflow.com/questions/11631717
复制相似问题