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

  1. Install ext-mongodb 2.x (e.g. pecl install mongodb).
  2. In a Drupal 10.5+/11 project, run composer require drupal/mongodb.
  3. Composer refuses to install because drupal/mongodb requires ext-mongodb ^1.5.1 and the installed version is 2.x.

Proposed resolution

  • Widen the constraints in the root and all submodule composer.json files 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:

    A first pass found no uses of Collection::count() or mapReduce(). The module already uses countDocuments(), and its typeMap options and Database::command() calls should be checked against 2.x.

  • Update the version notes and feature checks in MongoDb.php and the docs where they refer to 1.x library versions.
  • Consider removing --ignore-platform-req=ext-mongodb from .gitlab-ci.yml once the constraints allow 2.x. CI already runs pecl 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

Command icon 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

yurii novak created an issue. See original summary.

fgm’s picture

Status: Active » Postponed (maintainer needs more info)

Hi @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/mongodb 2.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.

matthand’s picture

Assigned: Unassigned » matthand
Status: Postponed (maintainer needs more info) » Active

I'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.

matthand’s picture

Issue summary: View changes
matthand’s picture

Issue summary: View changes

matthand’s picture

Status: Active » Needs review

I've opened an MR, which allows ext-mongodb 2.x and mongodb/mongodb 2.x alongside the existing 1.x support.

Changes

  • Widened the constraints in the root and all submodule composer.json files 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.
  • Updated docs/install.md to 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, and Database::listCollections() now returns a plain Iterator.

Logger::eventCollections() in mongodb_watchdog declared CollectionInfoIterator as its return type, so it would fail on 2.x. I changed the return type to \Iterator. In 1.x, CollectionInfoIterator extends Iterator, so this works on both versions. CollectionInfoIterator adds 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 its bsonUnserialize() methods already declare : void. It also doesn't use mapReduce(), the removed BSON functions, integer ReadPreference modes, or the removed query and driver options.

Testing

Composer resolves mongodb/mongodb 2.4.2 against ext-mongodb 2.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, because pecl install mongodb installs 2.x. Manual testing with the 1.x extension and library would still be welcome.

Setting to Needs review.

matthand’s picture

The first pipeline didn't really test 2.x: pecl install mongodb failed because the CI image lacks autoconf, so the extension never got installed. I've updated .gitlab-ci.yml to install the build tools and to fail the job if the extension doesn't load.

The failures in ControllerTest (drupal_rewrite_settings()) and FormStateValueResolver (ArgumentValueResolverInterface) are unrelated Drupal 11 issues and should have a separate follow-up for them.

matthand’s picture

With the CI fix, the kernel tests now run against ext-mongodb 2.5.3 and mongodb/mongodb 2.x, and all tests that talk to MongoDB pass. The remaining failures (drupal_rewrite_settings(), ArgumentValueResolverInterface, the QueueTest constructor, non-static data providers) are existing Drupal 11 / PHPUnit 10 issues, unrelated to this change.

matthand’s picture

Category: Support request » Feature request
matthand’s picture

Status: Needs review » Postponed

Moving this to postponed and draft until MR51 merges. Then will need to adjust this MR a little before it's ready again.

matthand’s picture

Issue summary: View changes
fgm’s picture

Status: Postponed » Needs work

I 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.json changes applied on top of the #3625939: Fix Drupal 11 / Symfony 7 compatibility errors and failing CI jobs branch.

  • Result: 50 tests, 207 assertions, 1 error. Every test that talks to MongoDB passes with the 2.x driver.
  • The one error is Functional ControllerTest::testLoggerReportsAccess: on PHP 8.5, the implicitly nullable $eventTemplate in DetailController::getTop() fails the test, where PHP 8.4 only reported it. That is #3458044: [PHP 8.4] Fix implicitly nullable type declarations.
  • PHP 8.5 also deprecates ReflectionMethod::setAccessible(), used in QueueTest.php line 176. It has had no effect since PHP 8.1, so the line can simply go.
  • The 1.x extension does not build on PHP 8.5: mongodb-1.21.10 fails on IS_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:

fgm’s picture

Status: Needs work » Reviewed & tested by the community

Mostly 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.

  • fgm committed af62e8c4 on 8.x-2.x authored by matthand
    Draft: Resolve Issue #3542043: Allow compatibility with ext-mongodb...
fgm’s picture

Status: Reviewed & tested by the community » Fixed

Merged to today's HEAD, thanks.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

matthand’s picture

Yeah so cool!!! Nice @fgm!!!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.