The MongoDB\Driver\Cursor class
(mongodb >=1.0.0)
Введение
The MongoDB\Driver\Cursor class encapsulates the results of a MongoDB command or query and may be returned by MongoDB\Driver\Manager::executeCommand() or MongoDB\Driver\Manager::executeQuery(), respectively.
Обзор классов
MongoDB\Driver\Cursor
implements
Traversable
{
/* Методы */
}Содержание
- MongoDB\Driver\Cursor::__construct — Create a new Cursor (not used)
- MongoDB\Driver\Cursor::getId — Returns the ID for this cursor
- MongoDB\Driver\Cursor::getServer — Returns the server associated with this cursor
- MongoDB\Driver\Cursor::isDead — Checks if the cursor is still open on the server
- MongoDB\Driver\Cursor::setTypeMap — Sets a type map to use for BSON unserialization
- MongoDB\Driver\Cursor::toArray — Returns an array containing all results for this cursor
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- MongoDB
- MongoDB\Driver\Manager
- MongoDB\Driver\Command
- MongoDB\Driver\Query
- MongoDB\Driver\BulkWrite
- MongoDB\Driver\WriteConcern
- MongoDB\Driver\ReadPreference
- MongoDB\Driver\ReadConcern
- MongoDB\Driver\Cursor
- MongoDB\Driver\CursorId
- MongoDB\Driver\Server
- MongoDB\Driver\WriteConcernError
- MongoDB\Driver\WriteError
- MongoDB\Driver\WriteResult
Коментарии
As one might notice, this class does not implement a hasNext() or next() method as opposed to the now deprecated Mongo driver.
If, for any reason, you need to pull data from the cursor procedurally or otherwise need to override the behavior of foreach while iterating on the cursor, the SPL \IteratorIterator class can be used. When doing so, it is important to rewind the iterator before using it, otherwise you won't get any data back.
<?php
$cursor = $collection->find();
$it = new \IteratorIterator($cursor);
$it->rewind(); // Very important
while($doc = $it->current()) {
var_dump($doc);
$it->next();
}
?>
I used this trick to build a backward compatibility wrapper emulating the old Mongo driver in order to upgrade an older codebase.
I noticed that ->sort is missing from the cursor. Seems like the old driver has more functionality.
[red.: The way how cursors are created is different between the drivers. In the old driver, the cursor would not be created until after the first rewind() call on the iterator.
In the new driver the cursor already exists. Because sort (and limit and skip) parameters need to be send to the server, they can not be called after the cursor already has been created.
You can use sort (and limit and skip) with the new driver as well, by specifying them as options to the Query as shown in this example: mongodb-driver-query.construct#refsect1-mongodb-driver-query.construct-examples]
If you find that it would be easier to use arrays (instead of objects) for the returned documents, add the following after executing your query:
$cursor->setTypeMap(['root' => 'array', 'document' => 'array', 'array' => 'array']);
There used to be a count() method in the old mongo extension (http://docs.php.net/manual/en/mongocursor.count.php), however, this feature seems to be deleted in mongodb.
I've seen some people use executeCommand() to do that, but I found it much more earier to just use the toArray() method and count the returned array.
For example:
$manager = new MongoDB\Driver\Manager();
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('db.collection', $query)->toArray();
var_dump(count($cursor));
Since php-mongodb version 1.9.0 Cursor implements Iterator, but if you need to support older versions too, you can conditionally wrap the cursor with IteratorIterator:
<?php
$iterator = $collection->find();
if (!($iterator implements Iterator)) {
$iterator = new \IteratorIterator($iterator);
}
?>