The MongoCursor class
(PECL mongo >=0.9.0)
Introduction
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:
Example #1 MongoCursor basic usage
<?php
$cursor = $collection->find();
var_dump(iterator_to_array($cursor));
?>
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:
Example #2 Iterating over MongoCursor
<?php
$cursor = $collection->find();
foreach ($cursor as $doc) {
// do something to each document
}
?>
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.
Example #3 Adding options to MongoCursor
<?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);
?>
Class synopsis
$connection
, string $ns
[, array $query
= array()
[, array $fields
= array()
]] )Static Variables
- slaveOkay
-
If the query should have the "slaveOkay" flag set, which allows reads on the secondary (secondaries are, by default, just for backup and not queried). Can be overridden with MongoCursor::slaveOkay().
This functionality is deprecated. Please use Read Preferences instead.
- timeout
-
Set timeout in milliseconds for all database responses. Use -1 to wait forever. Can be overridden with MongoCursor::timeout(). This does not cause the MongoDB server to cancel the operation; it only instructs the driver to stop waiting for a response and throw a MongoCursorTimeoutException after a set time.
Table of Contents
- MongoCursor::addOption — Adds a top-level key/value pair to a query
- MongoCursor::awaitData — Sets whether this cursor will wait for a while for a tailable cursor to return more data
- MongoCursor::batchSize — Limits the number of elements returned in one batch
- MongoCursor::__construct — Create a new cursor
- MongoCursor::count — Counts the number of results for this query
- MongoCursor::current — Returns the current element
- MongoCursor::dead — Checks if there are results that have not yet been sent from the database
- MongoCursor::doQuery — Execute the query
- MongoCursor::explain — Return an explanation of the query, often useful for optimization and debugging
- MongoCursor::fields — Sets the fields for a query
- MongoCursor::getNext — Advances the cursor to the next result, and returns that result
- MongoCursor::getReadPreference — Get the read preference for this query
- MongoCursor::hasNext — Checks if there are any more elements in this cursor
- MongoCursor::hint — Gives the database a hint about the query
- MongoCursor::immortal — Sets whether this cursor will timeout
- MongoCursor::info — Gets information about the cursor's creation and iteration
- MongoCursor::key — Returns the current result's _id, or its index within the result set
- MongoCursor::limit — Limits the number of results returned
- MongoCursor::maxTimeMS — Sets a server-side timeout for this query
- MongoCursor::next — Advances the cursor to the next result, and returns that result
- MongoCursor::partial — If this query should fetch partial results from mongos if a shard is down
- MongoCursor::reset — Clears the cursor
- MongoCursor::rewind — Returns the cursor to the beginning of the result set
- MongoCursor::setFlag — Sets arbitrary flags in case there is no method available the specific flag
- MongoCursor::setReadPreference — Set the read preference for this query
- MongoCursor::skip — Skips a number of results
- MongoCursor::slaveOkay — Sets whether this query can be done on a secondary [deprecated]
- MongoCursor::snapshot — Use snapshot mode for the query
- MongoCursor::sort — Sorts the results by given fields
- MongoCursor::tailable — Sets whether this cursor will be left open after fetching the last results
- MongoCursor::timeout — Sets a client-side timeout for this query
- MongoCursor::valid — Checks if the cursor is reading a valid result
Vertaling niet beschikbaar
De PHP-handleiding is nog niet in het Nederlands vertaald, dus het scherm is in het Engels. Als u wilt, kunt u het ook in het Frans of in het Duits raadplegen.
Als je de moed voelt, kun je je vertaling aanbieden ;-)
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 30/01/2003 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/php-rf-class.mongocursor.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.