4.2 Support is highlight of new MongoDB PHP drivers

Dj Walker-Morgan


The latest release of the MongoDB PHP driver has been updated with support for MongoDB 4.2. The driver, which comes in two parts, the low-level MongoDB PHP extension and the high-level MongoDB library, has been updated to versions 1.6 and 1.5 respectively.

The MongoDB PHP Extension

The low-level driver adds support for multi-document transactions on sharded clusters, a flagship MongoDB 4.2 feature, adding a number of methods for pinning a session to a mongos server and determining if a transaction is in progress. Other changes include enhancements to the RuntimeException class to expose error label information, useful for inferring whether an error allows a transaction to be retried.

Using the driver with MongoDB 3.6 or later, executeQuery and executeReadCommand can now automatically retry commands that fail thanks to the implementation of retryable read support. Support for both retryable reads and writes is now enabled by default in the new driver.

MongoDB 4.2's pipeline-powered updates are supported with the Bulkwrite::update() method, which can accept a pipeline as its second parameter.

Further details on this update can be found in the mongo-php-driver 1.6.0 release notes.

The MongoDB PHP Library

Built on top of the MongoDB PHP extension, the library's high-level abstraction of MongoDB also includes support for multi-document transactions on sharded clusters, along with a new convenient API for working with transactions.

Other changes include improved change stream support, allowing applications to get a resume token with the MongoDB\ChangeStream::getResumeToken() method, which can be used to recover from an error and restart iteration of a change stream from its last known position.

The extension's support for pipelines in updates in 4.2 is also available in the abstracted library with updateMany(), findOneAndUpdate() and other update methods able to accept a pipeline as an argument for the update process.

Further details on this update can be found in the mongo-php-library 1.5.0 release notes.

If you are looking for documentation, install instructions, or source code for either the MongoDB PHP extension or library, the essential resource to refer to is the MongoDB PHP driver ecosystem site which has links to all relevant information.