Наш чат в Telegram для обмена идеями, проектами, мыслями, людьми в сфере ИТ г.Ростова-на-Дону: @it_rostov

The MongoCursor class

(Информация о версии неизвестна, возможно, только в SVN)


A cursor is used to iterate through the results of a database query. For example, to query the database and see all results, you could do:

$cursor = $collection->find();

You don't generally create cursors using the MongoCursor constructor, you get a new cursor by calling MongoCollection::find() (as shown above).

Suppose that, in the example above, $collection was a 50GB collection. We certainly wouldn't want to load that into memory all at once, which is what a cursor is for: allowing the client to access the collection in dribs and drabs.

If we have a large result set, we can iterate through it, loading a few megabytes of results into memory at a time. For example, we could do:

$cursor = $collection->find();
foreach ($cursor as $doc) {
    // do something to each document

This will go through each document in the collection, loading and garbage collecting documents as needed.

Note that this means that a cursor does not "contain" the database results, it just manages them. Thus, if you print a cursor (with, say, var_dump() or print_r()), you'll just get the cursor object, not your documents. To get the documents themselves, you can use one of the methods shown above.

Cursor Stages

A MongoCursor has two "life stages": pre- and post- query. When a cursor is created, it has not yet contacted the database, so it is in its pre-query state. In this state, the client can further specify what they want the query to do, including adding limits, skips, sorts, and more advanced options.

When the client attempts to get a result (by calling MongoCursor::next(), directly or indirectly), the cursor moves into the post-query stage. At this point, the query has been executed by the database and cannot be modified anymore.

$cursor = $collection->find()->limit(10);
// database has not yet been queried, so more search options can be added
$cursor = $cursor->sort(array("a" => 1));
// now database has been queried and more options cannot be added
// so this will throw an exception:

Обзор классов

MongoCursor implements Iterator {
/* Static Fields */
static boolean $MongoCursor->slaveOkay = FALSE ;
static integer $timeout = 20000 ;
/* Методы */
public MongoCursor MongoCursor::addOption ( string $key , mixed $value )
public MongoCursor MongoCursor::batchSize ( int $num )
MongoCursor::__construct ( Mongo $connection , string $ns [, array $query = array() [, array $fields = array() ]] )
public int MongoCursor::count ([ bool $foundOnly = FALSE ] )
public array MongoCursor::current ( void )
public bool MongoCursor::dead ( void )
protected void MongoCursor::doQuery ( void )
public array MongoCursor::explain ( void )
public MongoCursor MongoCursor::fields ( array $f )
public array MongoCursor::getNext ( void )
public bool MongoCursor::hasNext ( void )
public MongoCursor MongoCursor::hint ( array $key_pattern )
public MongoCursor MongoCursor::immortal ([ bool $liveForever = true ] )
public array MongoCursor::info ( void )
public string MongoCursor::key ( void )
public MongoCursor MongoCursor::limit ( int $num )
public void MongoCursor::next ( void )
public MongoCursor MongoCursor::partial ([ bool $okay = true ] )
public void MongoCursor::reset ( void )
public void MongoCursor::rewind ( void )
public MongoCursor MongoCursor::skip ( int $num )
public MongoCursor MongoCursor::slaveOkay ([ bool $okay = true ] )
public MongoCursor MongoCursor::snapshot ( void )
public MongoCursor MongoCursor::sort ( array $fields )
public MongoCursor MongoCursor::tailable ([ bool $tail = true ] )
public MongoCursor MongoCursor::timeout ( int $ms )
public bool MongoCursor::valid ( void )

Static Variables

MongoCursor::slaveOkay -

If the query should have the "slaveOkay" flag set, which allows reads on the slave (slaves are, by default, just for backup and unreadable). Can be overridden with MongoCursor::slaveOkay().

MongoCursor::timeout -

Set timeout in milliseconds for all database responses. To wait forever, use -1. Can be overridden with MongoCursor::timeout(). This does not cause the MongoDB server to cancel the operation, it just causes the driver to stop waiting for a response and throw a MongoCursorTimeoutException.


Смотрите также

MongoDB core docs on » cursors.

Описание класса mongocursor, примеры использования класса mongocursor.

Описание на ru2.php.net
Описание на php.ru