我需要在mongo php中使用类似的查询

raa*_*van 5 php regex mongodb

我在shell中使用过这个查询.它工作正常.如果我在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/>";
}
Run Code Online (Sandbox Code Playgroud)

我不确定,会db.base.find({"ro": /^a/}).limit(10);在php中查询工作吗?

Jus*_*ins 6

你需要使用MongoRegex()......

请参阅此处的文档:http://php.net/manual/en/class.mongoregex.php

那么PHP代码就像......

$regex = new MongoRegex("/^a/"); 
$where = array('ro' => $regex);
$cursor = $collection->find($where);
Run Code Online (Sandbox Code Playgroud)