©
This document uses PHP Chinese website manual Release
我们可以用查询条件从集合中获得多个文档。 例如:想要获得 "i" > 50 的文档,我们需要:
<?php
$connection = new MongoClient ();
$collection = $connection -> database -> collectionName ;
$query = array( "i" => array( '$gt' => 50 ) ); //note the single quotes around '$gt'
$cursor = $coll -> find ( $query );
while ( $cursor -> hasNext () )
{
var_dump ( $cursor -> getNext () );
}
?>
将会显示 "i" > 50 的文档。也可以获得一个范围内的文档,比如 20 < i <= 30:
<?php $connection = new MongoClient(); $collection = $connection->database->collectionName;$query = array( 'i' => array( '$gt' => 20, "\$lte" => 30 ) ); $cursor = $coll->find( $query );while ( $cursor->hasNext() ) { var_dump( $cursor->getNext() ); } ?>
要注意美元字符始终都需要转义,或者可以用单引号代替。否则 PHP 会把它解析成对应的变量 $gt 。