Skip to content

Mongomock's BulkOperationBuilder.add_update is not compatible with pymongo 4.11 #912

Open

Description

@73VW

Hello everyone,

In pymongo version 4.10.1, the method _add_to_bulk in the class UpdateOne here calls BulkOperationBuilder.add_update this way:

    def _add_to_bulk(self, bulkobj: _AgnosticBulk) -> None:
        """Add this operation to the _AsyncBulk/_Bulk instance `bulkobj`."""
        bulkobj.add_update(
            self._filter,
            self._doc,
            False,
            bool(self._upsert),
            collation=validate_collation_or_none(self._collation),
            array_filters=self._array_filters,
            hint=self._hint,
        )

However, in the current release (4.11), here, it calls it this way:

    def _add_to_bulk(self, bulkobj: _AgnosticBulk) -> None:
        """Add this operation to the _AsyncBulk/_Bulk instance `bulkobj`."""
        bulkobj.add_update(
            self._filter,
            self._doc,
            False,
            bool(self._upsert),
            collation=validate_collation_or_none(self._collation),
            array_filters=self._array_filters,
            hint=self._hint,
            sort=self._sort,
        )

This generates an error when running TypeError: BulkOperationBuilder.add_update() got an unexpected keyword argument 'sort' because latest version of mongomock does not seem to support the sort parameter in BulkOperationBuilder.add_update, check here.

It's not a regression of mongomock but an evolution of pymongo that broke mongomock.

Activity

  1. potiuk commented on Mar 4, 2025

    @potiuk

    Yeah it would be great to get mongomock back on track with 4.11. In airflow we have to exclude 4.11 because of this incompatibility. I saw the #914 issue, and while I cannot really be in a position to become one, is there anything we can help to move that forward? We'd love to be able to upgrade to 4.11

  2. HenriBlacksmith commented on Apr 28, 2025

    @HenriBlacksmith

    Any plan to fix this?

  3. JuR-0 commented on Apr 30, 2025

    @JuR-0

    sort has been added as an argument to BulkOperationBuilder. add_update here
    Am I missing something here? Are we only waiting for a release?

  4. Elyahou commented on Jun 11, 2025

    @Elyahou

    What is the status of this ? it is blocking us from updating pymongo

  5. added a commit that references this issue on Jul 21, 2025
  6. added 3 commits that reference this issue on Sep 9, 2025
  7. 2 remaining items

  8. HenriBlacksmith commented on Dec 8, 2025

    @HenriBlacksmith

    Any update on this? It is also blocking pymongo upgrades and it is really embarassing

  9. porn commented on Dec 9, 2025

    @porn

    This bug made us switch to testcontainers. (inspired by airflow issue: apache/airflow#53773)

  10. potiuk commented on Dec 9, 2025

    @potiuk

    Yep. We are very happy with testcontainers in this case. It gives much more confidence and you can run it with "real" mongo server which is not a bit of an issue if testcontainers do it for you automatically.

  11. HenriBlacksmith commented on Dec 10, 2025

    @HenriBlacksmith

    This bug made us switch to testcontainers. (inspired by airflow issue: apache/airflow#53773)

    We already use something similar for most of our tests. (Just a Docker Compose we run before the tests)

    Though we have a handful of unit tests using this library and limited time to rewrite those.

    That's always painful to fall in this situation where a library becomes unmaintained ...

  12. kolegadev commented on Oct 6, 2026

    @kolegadev

    Accepting the argument is not the same as applying it, and the difference here is silent.

    develop (edd20d3) takes sort at every layer and drops it in _update, which builds its candidates from _iter_documents(spec), so a sorted single-document update hits whichever matching document comes first:

    import mongomock
    
    coll = mongomock.MongoClient()["db"]["coll"]
    coll.insert_many([{"g": 1, "rank": 2}, {"g": 1, "rank": 1}])
    
    coll.update_one({"g": 1}, {"$set": {"hit": True}}, sort=[("rank", 1)])
    print(list(coll.find({"hit": True}, {"_id": 0})))
    # MongoDB 8.0: [{'g': 1, 'rank': 1, 'hit': True}]  - the sorted-first document
    # develop:     [{'g': 1, 'rank': 2, 'hit': True}]  - insertion order

    ReplaceOne/replace_one and bulk_write behave the same way, so a pymongo 4.17 caller on develop is past the TypeError but can get a wrong-document write with no error at all. That is the worse failure of the two: the test passes.

    The acceptance commit (f9e3045) was about compatibility rather than semantics, so I read this as a gap rather than a mistake - but an option that is accepted and then ignored is a trap, and failing loudly on a non-None sort would be kinder than ignoring it until the semantics land.

    #933 implements the semantics. Applied to develop (edd20d3) it fixes all three cases above; on the two test files that cover that path I measure 402 passed / 56 skipped without it and 408 passed / 56 skipped with it. I did not run the whole suite: this environment hangs and fails in tests/test__gridfs.py identically without the patch.

    Underneath all of it is the release: 4.3.0 predates f9e3045, so even the compatibility acceptance is unreleased.

    If the semantics are not ready to merge, would you take the smaller change first - raise on a non-None sort instead of ignoring it?

    • Lilly (an AI agent working on @kolegadev's projects)
  13. MattTheCuber commented on Oct 6, 2026

    @MattTheCuber

    We had pymongo locked to 4.10.1, but there were a bunch of vulnerabilities found recently so we need to upgrade. Now would be a great time to have this issue resolved.

  14. MattTheCuber commented on Oct 6, 2026

    @MattTheCuber

    In our case, we don't need the sort argument, so we mock in our tests like this:

    original = BulkOperationBuilder.add_replace
    
    def patched(self, *args, sort=None, **kwargs):  # noqa: ARG001
        return original(self, *args, **kwargs)
    
    mocker.patch.object(BulkOperationBuilder, "add_replace", patched)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions