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
If I would like (to rent) a desk for working for an afternoon in Amsterdam North, or near Centraal, what would be a good place?
Updated a marina
I walked 9.7km in 1h46m04s
Updated an information and a bench
Created 2 benches
Created 4 picnic_tables, a bench, and a fitness_station; Updated a pub and a sport club
Created 2 benches and 2 waste_baskets; Updated 4 benches, a bus_stop, and a cafe; Confirmed a dentist
Created 3 waste_baskets, 2 main entrances, and a bench; Deleted a cycle_barrier, a bench, and a waste_basket
Created an information; Updated a waste_basket and a bench
Updated 2 waste_baskets and a bench
Created 3 benches
Created a waste_basket; Updated 2 benches and a tree
I hiked 17.0km in 3h52m14s
I walked 3.5km in 35m31s
Created a main entrance and a home entrance
Created an entrance
Updated a house building
Created an entrance
I walked 5.8km in 1h15m06s
I've just finished reading "A Cheese-Monger's Tour de France", by Ned Palmer.
Now I want to try many of those! 🧀
I'm thrilled to announce that I'll be speaking at the 23rd edition of #phpday, the international PHP conference in Italy, organised by @grusp.
I’ll be presenting a talk titled: "Better Debugging With Xdebug".
It's in Verona, Italy, on May 14-15th 2026.
You can use my speaker’s discount code "speaker_10OFF" for 10% off at https://www.phpday.it/tickets/?utm_medium=organic&utm_source=linkedin&utm_campaign=post-speaker
I walked 5.5km in 1h11m00s
I walked 1.1km in 9m37s
Merged pull request #1066
PHP 8.6: Changes to opcache optimisations wrt function arguments


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