One *VERY* important note, if You do:
$cursor = $collection->find(array(), array('_id' => 0)); // ommit '_id' field in result
then:
var_dump(iterator_to_array($cursor));
will return only *ONE* document, not all!
The MongoCursor class
Увод
Result object for database query.
The database is not actually queried until next() or hasNext() is called. Before the database is queried, commands can be strung together, as in:
<?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
implements
Iterator
{
/* Static Fields */
static
boolean
$slaveOkay
= FALSE
;
/* Methods */
__construct
( resource $connection
, string $ns
[, array $query = array()
[, array $fields = array()
]] )
}Съдържание
- MongoCursor::__construct — Create a new cursor
- MongoCursor::count — Counts the number of results for this query
- MongoCursor::current — Returns the current result
- MongoCursor::doQuery — Execute the query.
- MongoCursor::explain — Return an explanation of the query, often useful for optimization and debugging
- MongoCursor::getNext — Return the next object to which this cursor points, and advance the cursor
- MongoCursor::hasNext — Checks if there are any more elements in this cursor
- MongoCursor::hint — Gives the database a hint about the query
- MongoCursor::key — Returns the current result's _id
- MongoCursor::limit — Limits the number of results returned
- MongoCursor::next — Advances the cursor to the next result
- MongoCursor::reset — Clears the cursor
- MongoCursor::rewind — Returns the cursor to the beginning of the result set
- MongoCursor::skip — Skips a number of results
- MongoCursor::slaveOkay — Sets whether this query can be done on a slave
- MongoCursor::sort — Sorts the results by given fields
- MongoCursor::tailable — Sets whether this cursor will be left open after fetching the last results
- MongoCursor::valid — Checks if the cursor is reading a valid result.
adrian dot zurek at netmedia dot com dot pl
24-Oct-2011 04:08
Adil Baig @ AiDezigns
07-Sep-2011 07:28
If you want to know whether a cursor returned any results it is faster to use 'hasNext()' than 'count'
