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 = $collection->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 = $collection->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.
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- MongoDB
- Manual
- Making a Connection
- Getting a Database
- Getting A Collection
- Inserting a Document
- Finding Documents using MongoCollection::findOne
- Adding Multiple Documents
- Counting Documents in A Collection
- Using a Cursor to Get All of the Documents
- Setting Criteria for a Query
- Getting A Set of Documents With a Query
- Creating An Index
Коментарии
Note that in the example...
<?php $cursor = $coll->find( $query ); ?>
should of course be...
<?php $cursor = $collection->find( $query ); ?>