我已将正则表达式保存为集合中的值。我想检查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;
&GT;
它的工作但什么都不返回....!
共鸣:
Array
(
)
这里,安慰屏幕排序,给定返回值。
这里是我的数据样本:
怎么了?
答案 0 :(得分:0)
如果我的上述评论不起作用,请尝试更改此内容:
$cursor = $db->$c->find($query);
为:
$cursor = $db->selectCollection($c)->find($query);
答案 1 :(得分:0)
最后我得到了答案:
更改下面显示的代码
<?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
)