(PECL mongo >=0.9.0)
Examples:
MongoCursor basic usage

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:

<?php

$cursor = $collection->find();
var_dump(iterator_to_array($cursor));

?>

Iterating over MongoCursor

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:

<?php

$cursor = $collection->find();

foreach ($cursor as $doc) {
    // do something to each document
}

?>

Adding options to MongoCursor

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.

<?php

$cursor = $collection->find()->limit(10);

// database has not yet been queried, so more search options can be added
$cursor = $cursor->sort(array("a" => 1));

var_dump($cursor->getNext());
// now database has been queried and more options cannot be added

// so this will throw an exception:
$cursor->skip(4);
?>

MongoCursor::rewind

(PECL mongo >=0.9.0) Returns the cursor to the beginning of the result set

2016-02-24 16:20:43
MongoCursor::awaitData

(PECL mongo >=1.2.11) Sets whether this cursor will wait for a while for a tailable cursor to return more

2016-02-24 16:20:41
MongoCursor::getReadPreference

(PECL mongo >=1.3.3) Get the read preference for this query

2016-02-24 16:20:42
MongoCursor::valid

(PECL mongo >=0.9.0) Checks if the cursor is reading a valid result.

2016-02-24 16:20:45
MongoCursor::key

(PECL mongo >=0.9.0) Returns the current result's _id, or its index within the result set

2016-02-24 16:20:43
MongoCursor::hasNext

(PECL mongo >=0.9.0) Checks if there are any more elements in this cursor

2016-02-24 16:20:42
MongoCursor::maxTimeMS

(PECL mongo >=1.5.0) Sets a server-side timeout for this query

2016-02-24 16:20:43
MongoCursor::slaveOkay

(PECL mongo >=0.9.4) Sets whether this query can be done on a secondary [deprecated]

2016-02-24 16:20:44
MongoCursor::hint

(PECL mongo >=0.9.0) Gives the database a hint about the query

2016-02-24 16:20:42
MongoCursor::sort

(PECL mongo >=0.9.0) Sorts the results by given fields

2016-02-24 16:20:45