我们可以建立一个查询,然后传递给 MongoCollection::find() 方法来查询集合的一个子集。 例如:我们要查询 "i" 字段等于 71 的文档,我们可以使用:
<?php
$connection = new MongoClient();
$collection = $connection->database->collectionName;
$query = array( 'i' => 71 );
$cursor = $collection->find( $query );
while ( $cursor->hasNext() )
{
var_dump( $cursor->getNext() );
}
?>
以上例程会输出:
array(2) {
["_id"]=>
object(MongoId)#6 (0) {
}
["i"]=>
int(71)
["_ns"]=>
"testCollection"
}