Tutorial
PHP 手册

Getting A Set of Documents With a Query

We can use the query to get a set of documents from our collection. For example, if we wanted to get all documents where "i" > 50, we could write:

<?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() );
}
?>

which should print the documents where "i" > 50. We could also get a range, say 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() );
}
?>

Remember to always escape the $-symbol or use single quotes. Otherwise PHP will interpret it to be the variable $gt.


Tutorial
PHP 手册