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.
クラス概要
変更履歴
バージョン | 説明 |
---|---|
PECL mongodb 1.9.0 | Implements Iterator. |
PECL mongodb 1.6.0 | Implements MongoDB\Driver\CursorInterface, which extends Traversable. |
例
例1 Reading a result set
MongoDB\Driver\Manager::executeCommand() and MongoDB\Driver\Manager::executeQuery() both return their result(s) as a MongoDB\Driver\Cursor object. This object can be used to iterate over the result set of the command or query.
Because MongoDB\Driver\Cursor implements the
Traversable interface, you can simply
iterate over the result set with
foreach
.
<?php
$manager = new MongoDB\Driver\Manager();
/* Insert some documents so that our query returns information */
$bulkWrite = new MongoDB\Driver\BulkWrite;
$bulkWrite->insert(['name' => 'Ceres', 'size' => 946, 'distance' => 2.766]);
$bulkWrite->insert(['name' => 'Vesta', 'size' => 525, 'distance' => 2.362]);
$manager->executeBulkWrite("test.asteroids", $bulkWrite);
/* Query for all the items in the collection */
$query = new MongoDB\Driver\Query( [] );
/* Query the "asteroids" collection of the "test" database */
$cursor = $manager->executeQuery("test.asteroids", $query);
/* $cursor now contains an object that wraps around the result set. Use
* foreach() to iterate over all the result */
foreach($cursor as $document) {
print_r($document);
}
?>
上の例の出力は、 たとえば以下のようになります。
stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5a4cff2f122d3321565d8cc2 ) [name] => Ceres [size] => 946 [distance] => 2.766 ) stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5a4cff2f122d3321565d8cc3 ) [name] => Vesta [size] => 525 [distance] => 2.362 }
例2 Reading a result set for a tailable cursor
» Tailable cursors are a special type of MongoDB cursor that allows the client to read some results and then wait until more documents become available. These cursors are primarily used with » Capped Collections and » Change Streams.
While normal cursors can be iterated once with foreach
,
that approach will not work with tailable cursors. When
foreach
is used with a tailable cursor, the loop will
stop upon reaching the end of the initial result set. Attempting to
continue iteration on the cursor with a second
foreach
would throw an exception, since PHP attempts to
rewind the cursor. Similar to result objects in other database drivers,
cursors in MongoDB only support forward iteration, which means they cannot
be rewound.
In order to continuously read from a tailable cursor, the Cursor object must be wrapped with an IteratorIterator. This allows the application to directly control the cursor's iteration, avoid inadvertently rewinding the cursor, and decide when to wait for new results or stop iteration entirely.
In order to demonstrate a tailable cursor in action, two scripts will be used: a "producer" and a "consumer". The producer script will create a new capped collection using the » create command and proceed to insert a new document into that collection each second.
<?php
$manager = new MongoDB\Driver\Manager;
$manager->executeCommand('test', new MongoDB\Driver\Command([
'create' => 'asteroids',
'capped' => true,
'size' => 1048576,
]));
while (true) {
$bulkWrite = new MongoDB\Driver\BulkWrite;
$bulkWrite->insert(['createdAt' => new MongoDB\BSON\UTCDateTime]);
$manager->executeBulkWrite('test.asteroids', $bulkWrite);
sleep(1);
}
?>
With the producer script still running, a second consumer script may be
executed to read the inserted documents using a tailable cursor, indicated
by the tailable
and awaitData
options
to MongoDB\Driver\Query::__construct().
<?php
$manager = new MongoDB\Driver\Manager;
$query = new MongoDB\Driver\Query([], [
'tailable' => true,
'awaitData' => true,
]);
$cursor = $manager->executeQuery('test.asteroids', $query);
$iterator = new IteratorIterator($cursor);
$iterator->rewind();
while (true) {
if ($iterator->valid()) {
$document = $iterator->current();
printf("Consumed document created at: %s\n", $document->createdAt);
}
$iterator->next();
}
?>
The consumer script will start by quickly printing all available documents
in the capped collection (as if foreach
had been used);
however, it will not terminate upon reaching the end of the initial result
set. Since the cursor is tailable, calling
IteratorIterator::next() will block and wait for
additional results. IteratorIterator::valid() is also
used to check if there is actually data available to read at each step.
注意: This example uses the
awaitData
query option to instruct the server to block for a short period (e.g. one second) at the end of the result set before returning a response to the driver. This is used to prevent the driver from aggressively polling the server when there are no results available. ThemaxAwaitTimeMS
option may be used in conjunction withtailable
andawaitData
to specify the amount of time that the server should block when it reaches the end of the result set.
エラー / 例外
When iterating over the cursor object, BSON data is converted into PHP variables. This iteration can cause the following Exceptions:
- Throws MongoDB\Driver\Exception\InvalidArgumentException if a class in the type map cannot be instantiated or does not implement MongoDB\BSON\Unserializable.
- Throws MongoDB\Driver\Exception\UnexpectedValueException if the input did not contain exactly one BSON document. Possible reasons include, but are not limited to, invalid BSON, extra data (after reading one BSON document), or an unexpected » libbson error.
目次
- MongoDB\Driver\Cursor::__construct — Create a new Cursor (not used)
- MongoDB\Driver\Cursor::current — Returns the current element
- 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 exhausted or may have additional results
- MongoDB\Driver\Cursor::key — Returns the current result's index within the cursor
- MongoDB\Driver\Cursor::next — Advances the cursor to the next result
- MongoDB\Driver\Cursor::rewind — Rewind the cursor to the first result
- 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
- MongoDB\Driver\Cursor::valid — Checks if the current position in the cursor is valid
User Contributed Notes 5 notes
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.
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']);
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: http://php.net/manual/en/mongodb-driver-query.construct.php#refsect1-mongodb-driver-query.construct-examples]
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);
}
?>