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
Put new open stretch of Canterbury Road on map
Created a bench and a crossing
Created 2 benches and a crossing; Updated 5 cushions, a post_box, and a crossing
Created a crossing; Updated a cushion
If you were wondering whether the www.php.net & downloads.php.net services weren't responding very well in the last 6 hours — thousands of requests/sec to https://www.php.net/ 's root.
The server's load was 720, didn't die, but CDN connections to it timed out.
Now there is a caching strategy in place for a selected set of resources.
Updated a bench
Created 3 benches; Updated 10 benches
Updated a bench
Updated a bus_stop
Created a bench and a waste_basket; Updated 6 bus_stops and a crossing
Created 2 waste_baskets and a recycling; Updated 2 bicycle_parkings and a recycling
Updated a fast_food, a funeral_directors shop, and 2 other objects; Confirmed a fast_food and a hairdresser shop
Created an information; Updated 3 benches and 2 waste_baskets
Updated 2 benches and a waste_basket
Updated a bench
Created a waste_basket and an information
Created a waste_basket
I hiked 18.0km in 4h1m52s
I walked 1.4km in 17m19s
I walked 4.5km in 1h21m49s
I just made and ate, a bowl full of bacon fried Brussels Sprouts. Not under duress, and out of my own free will.
Added new residential building
Created a hairdresser shop; Confirmed a convenience shop and a dry_cleaning shop
Created a building_materials shop, a vacant shop, and 4 other objects; Confirmed a hairdresser shop, a cafe, and 2 other objects



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