Problem/Motivation
The module's Composer constraints only allow version 1.x of the MongoDB PHP extension and library:
"ext-mongodb": "^1.5.1""mongodb/mongodb": "^1.13"
These constraints appear in the root composer.json and in the composer.json of each submodule (mongodb, mongodb_storage, mongodb_watchdog).
ext-mongodb 2.0.0 was released with incompatible changes to the extension API and dropped support for PHP 8.0. It must be used with the matching mongodb/mongodb 2.x library. Sites whose hosts ship ext-mongodb 2.x can't install the module without --ignore-platform-req=ext-mongodb. pecl install mongodb now installs 2.x by default.
As noted in the comments below, the 2.x driver drops support for MongoDB server 4.x. The maintainer considers that acceptable.
The module already requires PHP >= 8.1, so the 2.x driver's PHP requirement is not a blocker.
Steps to reproduce
- Install
ext-mongodb2.x (e.g.pecl install mongodb). - In a Drupal 10.5+/11 project, run
composer require drupal/mongodb. - Composer refuses to install because
drupal/mongodbrequiresext-mongodb ^1.5.1and the installed version is 2.x.
Proposed resolution
- Widen the constraints in the root and all submodule
composer.jsonfiles to:
"ext-mongodb": "^1.5.1 || ^2.0", "mongodb/mongodb": "^1.13 || ^2.0"
Composer will then pair the library with the installed extension: 1.x with 1.x, 2.x with 2.x.
- Review the 2.0.0 release notes and upgrade guides for removed or changed APIs that the module uses:
- https://github.com/mongodb/mongo-php-driver/releases/tag/2.0.0
- https://github.com/mongodb/mongo-php-library/releases/tag/2.0.0
A first pass found no uses of
Collection::count()ormapReduce(). The module already usescountDocuments(), and itstypeMapoptions andDatabase::command()calls should be checked against 2.x. - Update the version notes and feature checks in
MongoDb.phpand the docs where they refer to 1.x library versions. - Consider removing
--ignore-platform-req=ext-mongodbfrom.gitlab-ci.ymlonce the constraints allow 2.x. CI already runspecl install mongodb, which installs the 2.x extension.
Remaining tasks
- Review the 2.0 release notes and upgrade guides (extension and library).
- Create an issue fork / MR that widens the constraints.
- Fix any incompatibilities found.
- Run the test suite against both 1.x and 2.x (extension + library).
- Update documentation.
- Review and commit.
User interface changes
None.
API changes
None to the module's public API are expected. Sites using the 2.x driver get the 2.x mongodb/mongodb library. Custom code that calls the library directly through the module's client/database services may need changes from the library's 2.0 upgrade guide.
Data model changes
None.
AI disclosure
This issue was revised using Claude Code throughout authoring the issue update, with human review, manual edits and decisions made independently, per Drupal.org's AI contribution policy.
Issue fork mongodb-3542043
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
fgmHi @yurii. Have you already taken a look at what that would involve ?
The ext-mongodb 2.0.0 announcement documents incompatible changes in the extension API, dropping support for PHP 8.0 (no big deal, Drupal ≥ 10 already requires 8.1), and requiring
mongodb/mongodb2.0.0.The mongodb/mongodb 2.0.0 announcement is not as explicitly incompatible but still warns about many changes.
MongDB 4.x support is also dropped, but I do not think this is a big deal these days.
Comment #3
matthandI'm going to review the release notes and see if there's any obvious flags. I will also start an issue fork so we can loosen the version restraint to allow v2 to install and perform testing.
Comment #4
matthandComment #5
matthandComment #7
matthandI've opened an MR, which allows
ext-mongodb2.x andmongodb/mongodb2.x alongside the existing 1.x support.Changes
composer.jsonfiles to"ext-mongodb": "^1.5.1 || ^2.0"and"mongodb/mongodb": "^1.13 || ^2.0". Composer picks the library version that matches the installed extension.docs/install.mdto say the 2.x extension is supported.The one breaking change
I reviewed the module against the 2.0 upgrade guides for both the library and the extension. Only one change affects the module: the 2.0 library removed
MongoDB\Model\CollectionInfoIterator, andDatabase::listCollections()now returns a plainIterator.Logger::eventCollections()inmongodb_watchdogdeclaredCollectionInfoIteratoras its return type, so it would fail on 2.x. I changed the return type to\Iterator. In 1.x,CollectionInfoIteratorextendsIterator, so this works on both versions.CollectionInfoIteratoradds no methods of its own, so callers that only iterate the result are not affected.Nothing else from the upgrade guides affects the module. It doesn't use the return values of methods that now return
void(drop(),createCollection()), and itsbsonUnserialize()methods already declare: void. It also doesn't usemapReduce(), the removed BSON functions, integerReadPreferencemodes, or the removed query and driver options.Testing
Composer resolves
mongodb/mongodb2.4.2 againstext-mongodb2.5.2 and can still pick the 1.x library with the 1.x extension. CI should now run the 2.x library with the 2.x extension; it previously ran the 1.x library with the 2.x extension, becausepecl install mongodbinstalls 2.x. Manual testing with the 1.x extension and library would still be welcome.Setting to Needs review.
Comment #8
matthandThe first pipeline didn't really test 2.x:
pecl install mongodbfailed because the CI image lacksautoconf, so the extension never got installed. I've updated.gitlab-ci.ymlto install the build tools and to fail the job if the extension doesn't load.The failures in
ControllerTest(drupal_rewrite_settings()) andFormStateValueResolver(ArgumentValueResolverInterface) are unrelated Drupal 11 issues and should have a separate follow-up for them.Comment #9
matthandWith the CI fix, the kernel tests now run against
ext-mongodb2.5.3 andmongodb/mongodb2.x, and all tests that talk to MongoDB pass. The remaining failures (drupal_rewrite_settings(),ArgumentValueResolverInterface, theQueueTestconstructor, non-static data providers) are existing Drupal 11 / PHPUnit 10 issues, unrelated to this change.Comment #10
matthandComment #11
matthandMoving this to postponed and draft until MR51 merges. Then will need to adjust this MR a little before it's ready again.
Comment #12
matthandComment #13
fgmI tested this MR combined with the one from #3625939: Fix Drupal 11 / Symfony 7 compatibility errors and failing CI jobs, since the branch alone still hits the Drupal 11 breakages that one fixes. Setup: Drupal 11.4.8, PHP 8.5.11, ext-mongodb 2.5.3, mongodb/mongodb 2.4.2, MongoDB 8.3, with this MR's
composer.jsonchanges applied on top of the #3625939: Fix Drupal 11 / Symfony 7 compatibility errors and failing CI jobs branch.ControllerTest::testLoggerReportsAccess: on PHP 8.5, the implicitly nullable$eventTemplateinDetailController::getTop()fails the test, where PHP 8.4 only reported it. That is #3458044: [PHP 8.4] Fix implicitly nullable type declarations.ReflectionMethod::setAccessible(), used inQueueTest.phpline 176. It has had no effect since PHP 8.1, so the line can simply go.mongodb-1.21.10fails onIS_INTERNED, which PHP 8.5 removed. Among the extension's releases, only 2.1.2 and later announce PHP 8.5 support. Since Drupal 12 requires PHP 8.5, this issue is a prerequisite for Drupal 12.Once #3625939: Fix Drupal 11 / Symfony 7 compatibility errors and failing CI jobs is merged, please rebase and reduce this MR to what is specific to 2.x:
Logger::eventCollections()change: #3625939: Fix Drupal 11 / Symfony 7 compatibility errors and failing CI jobs makes the same one."ext-mongodb": "^1.21 || ^2.4"and"mongodb/mongodb": "^1.21 || ^2.4"in all fourcomposer.json. Library 2.4.2 requires ext-mongodb ^2.4, and these are the versions tested above.docs/install.mdhas been rewritten in #3626482: Update docs site for 2026-09, so the 2.x wording needs redoing on top of it: 8.x-2.x supports extension and library 1.21 or later, or 2.4 or later, and PHP 8.5 needs 2.x.--ignore-platform-req=ext-mongodb, Composer ignores the installed extension, so once 2.x is allowed it installs library 2.x even in jobs that run the 1.x extension. Installing the extension in the composer jobs too and dropping that flag lets Composer pick the matching library in each variant: 1.x with the pinned 1.21.10 on the current and previous cores, 2.x on the next major, where PHP 8.5 rules out 1.x.Comment #14
fgmMostly good, I added a few fixes, mostly around docs and the build.
Build only fails on #3458044: [PHP 8.4] Fix implicitly nullable type declarations, which I'll merge first.
Comment #16
fgmMerged to today's HEAD, thanks.
Comment #18
matthandYeah so cool!!! Nice @fgm!!!