New MongoDB Drivers for PHP and HHVM: Cursor Behaviour
We released a new version of the MongoDB driver for PHP (the mongodb extension) near the end of last year. In a previous blog post, I covered the back story of the how and why we undertook this effort. And in another post, I spoke about the architecture of the new driver. In this episode I will discuss changes in cursor behaviour.
I recently found the following comment added to the driver's documentation, on PHP.net, which read:
I noticed that
->sortis missing from the cursor. Seems like the old driver has more functionality.
The new driver certainly allows for sorting of results of queries, but no longer by calling the sort() method on the cursor object. This is because cursors are now created differently than in the old mongo extension.
Legacy Driver
In the old driver, the cursor would not really be created until after the first rewind() call on the iterator:
<?php $m = new MongoClient; // Select 'demo' database and 'example' collection $collection = $m->demo->example; // Create the cursor $cursor = $collection->find();
At this moment, although a cursor object had been created, the query had not yet executed (i.e. it was not sent to the server). The query would only be executed by starting iteration with foreach ( $cursor as $result ) or calling $cursor->rewind(). This gives you the chance to configure the cursor's query with sort(), limit(), and skip() before it is executed by the server:
// Add sort, and limit $cursor->sort( [ 'name' => 1 ] )->limit( 40 );
After the cursor starts iterating (through foreach or ->rewind()), you can no longer call the aforementioned methods, as well as other methods that configure the query, to modify the cursor.
New Driver
In the new driver, as soon as you have a \MongoDB\Driver\Cursor object, it has already been processed by the server. Because sort (and limit and skip) parameters need to be sent to the server before the query is executed, you can not retroactively call them on an existing Cursor object.
You can use sort (and limit and skip) with the new driver, but they must be specified as options to the \MongoDB\Driver\Query object before it is passed to \MongoDB\Driver\Manager::executeQuery():
<?php
$m = new \MongoDB\Driver\Manager();
$ns = 'demo.example';
// Create query object with all options:
$query = new \MongoDB\Driver\Query(
[], // query (empty: select all)
[ 'sort' => [ 'name' => 1 ], 'limit' => 40 ] // options
);
// Execute query and obtain cursor:
$cursor = $manager->executeQuery( $ns, $query );
Life Line
@milh0use You were on the news!
But the best thing I saw on my stroll through London's Richmond Park yesterday, was finding a nest of Sparrowhawks.
There was one parent and two juveniles.
First they were soaring overhead, before one of them landed in a tree right in front of me.
A while later, I saw them in the air again, where one of them let go of some food, which another grabbed right from the air.
#BirdsOfFediverse #Birds #BirdPhotography #Nature #NaturePhotography #London #NoAI
I had a stroll through London's Richmond Park yesterday.
I found quite a few Green and Great-Spotted Woodpeckers!
#BirdsOfFediverse #Birds #BirdPhotography #Nature #NaturePhotography #London #NoAI
I hiked 14.1km in 4h40m00s
Created a restaurant
I walked 1.6km in 23m28s
Merge branch 'xdebug_3_5'
Merged pull request #1099
Fixed issue #2435: Xdebug does not handle 'muiltiple_sessions' dbgp f…
I walked 6.2km in 1h5m12s
Merged pull request #1098
PHP 8.6: require et all no longer show the file names in error messag…
Created 2 gates, a bench, and a crossing; Updated 5 gates and 2 benches
I walked 6.1km in 1h19m29s
I walked 0.9km in 9m06s
Updated a restaurant
I walked 8.5km in 1h33m20s
I walked 7.0km in 1h12m36s
Staring Contest with an Owl
This Little Owl stared at me intensely for a fair amount of time in London's Bushy Park.
#BirdPhotography #BirdsOfFediverse #NaturePhotography #Photography #SuperbOwl #London #Birds
Updated an apartments building
I walked 6.8km in 1h6m55s
Little Owl owlet
This lovely owlet was quite easy to find in a London Park, with some help as they camouflage so well!
I saw its sibling too.
#BirdPhotography #BirdsOfFediverse #Photography #Nature #London #BirdsOfMastodon
I walked 10.8km in 2h3m32s
I went to a park (Bushy Park) to look for some owls.
I found Owls (Little and Tawny), but also a Green Woodpecker and two Kingfishers.
Snaps from my camera screen, and real photos will follow, but not of the Kingfishers as they were too far away and the photos are blurry.
#london #BirdPhotogaphy #BirdsOfMastodon #Birds #photography #BirdsOfFediverse #BushyPark
I hiked 13.8km in 4h44m12s






Shortlink
This article has a short URL available: https://drck.me/newmongo3-cer