2011-02-15 29 views
5

我已经在shell中使用了这个查询。它运行良好。如果我在PHP中使用它,它让我看到一个错误我需要在mongo中使用一个类似的查询php

<?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/>"; 
} 

我不知道,会在PHP db.base.find({"ro": /^a/}).limit(10);查询工作?

+1

什么错误?无论何时您说出“显示错误”这样的内容,确实有助于实际发布错误消息。 – netcoder 2011-02-15 21:45:02

回答

6

您需要使用MongoRegex()...

查看文档在这里:http://php.net/manual/en/class.mongoregex.php

所以PHP代码会是这样的......

$regex = new MongoRegex("/^a/"); 
$where = array('ro' => $regex); 
$cursor = $collection->find($where); 
-1

在一个行:

$mCollection->find(array("date" => new MongoRegex("/2011-10-25/"))); 
0

如果您使用mongodb扩展,您应该使用MongoDB \ BSON \ Regex()而不是MongoRegex()

$regex = new \MongoDB\BSON\Regex("^a"); 
$where = array('ro' => $regex); 
$cursor = $collection->find($where) 

你不必使用“/”[正则表达式] “/”。只是正则表达式工作正常