MongoDB\Driver
PHP Manual

The MongoDB\Driver\Cursor class

(mongodb >=1.0.0)

Einführung

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.

Klassenbeschreibung

MongoDB\Driver\Cursor implements Traversable {
/* Methoden */
final private __construct ( void )
final public MongoDB\Driver\CursorId getId ( void )
final public MongoDB\Driver\Server getServer ( void )
final public bool isDead ( void )
final public void setTypeMap ( array $typemap )
final public array toArray ( void )
}

Beispiele

Beispiel #1 Reading a result set

MongoDB\Driver\Manager::executeCommand() and MongoDB\Driver\Manager::executeQuery() both return their results as a MongoDB\Driver\Cursor object. This object can be used to iterator over the result set that the command or cursor returned.

Because this class implements the Traversable interface, you can simply iterator over the result set by using 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);
}

?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

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
}

Beispiel #2 Reading from a tailable cursors

Tailable cursors are special cursors in MongoDB that allow you to read results from a cursor, and then wait until more documents become available in the collection that match the query. This functionality only works with Capped Collections and » Change Streams, and requires a different way of iteration.

Where normal cursors can be iterated over with foreach(), the same does not work with tailable cursors as you can not call foreach() twice. In order to continuously read from a » Tailable Cursor, you will need to wrap the MongoDB\Driver\Cursor object with an IteratorIterator.

In the example below, we set up a » Capped Collection, into which we insert a few documents. We then run the query with the special cursor options tailable, awaitData and maxAwaitTimeMS. These options instruct the server to: keep the cursor open after the initial result set has been returned (tailable), and to block the read (awaitData) for at most maxAwaitTimeMS milliseconds. We then wrap the cursor in IteratorIterator and use IteratorIterator::valid() and IteratorIterator::next() to iterate over the result set.

<?php

$manager 
= new MongoDB\Driver\Manager();

/* Create capped collection "asteroids" on "test" database */
$manager->executeCommand("test", new MongoDB\Driver\Command([
    
'create' => "asteroids",
    
'capped' => true,
    
'size' => 1048576,
]));

/* 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, and make it tailable with a
 * maximum wait time of 10 seconds */
$query = new MongoDB\Driver\Query(
    [],
    [
        
'tailable' => true,
        
'awaitData' => true,
        
'maxAwaitTimeMS' => 10000,
    ]
);

/* Query the "asteroids" collection of the "test" database */
$cursor $manager->executeQuery("test.asteroids"$query);

/* Wrap cursor and rewind */
$iterator = new IteratorIterator($cursor);
$iterator->rewind();

/* Loop over the result set with valid() and next() */
while ($iterator->valid()) {
    
$document $iterator->current();
    echo 
date(DateTime::ISO8601), "\n";
    
print_r($document);

    
$iterator->next();
}

?>

During the second "Waiting", we insert another document into the collection. You can see on the date/time stamps that there are several seconds between the second and third result being returned.

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

2018-01-03T16:59:21+0000
stdClass Object
(
    [_id] => MongoDB\BSON\ObjectId Object
        (
            [oid] => 5a4d0be9122d3324914394e2
        )

    [name] => Ceres
    [size] => 946
    [distance] => 2.766
)

2018-01-03T16:59:21+0000
stdClass Object
(
    [_id] => MongoDB\BSON\ObjectId Object
        (
            [oid] => 5a4d0be9122d3324914394e3
        )

    [name] => Vesta
    [size] => 525
    [distance] => 2.362
)

2018-01-03T16:59:24+0000
stdClass Object
(
    [_id] => MongoDB\BSON\ObjectId Object
        (
            [oid] => 5a4d0becca67a2be8d10550c
        )

    [name] => Pallas
    [size] => 512
    [distance] => 2.773
)

Fehler/Exceptions

When iterating over the cursor object, BSON data is converted into PHP variables. This iteration can cause the following Exceptions:

Inhaltsverzeichnis


MongoDB\Driver
PHP Manual