From cac1be6e0cd72e55d8300d234c13bdf5294d0e7b Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Fri, 28 Feb 2025 10:48:36 -0800 Subject: [PATCH] PYTHON-5166 Allow Database.command to run bulkWrite commands (#2164) (cherry picked from commit 080c1c61212594e4de4792cf055b904a570c4359) --- doc/changelog.rst | 16 +++++++++++++++- pymongo/message.py | 2 +- test/asynchronous/test_database.py | 15 +++++++++++++++ test/test_database.py | 15 +++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/doc/changelog.rst b/doc/changelog.rst index a1bda74944..66f3efca88 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -1,7 +1,21 @@ Changelog ========= -Changes in Version 4.11.1 (2025/MM/DD) +Changes in Version 4.11.2 (YYYY/MM/DD) +-------------------------------------- + +Version 4.11.2 is a bug fix release. + +- Fixed a bug where :meth:`~pymongo.database.Database.command` would fail when attempting to run the bulkWrite command. + +Issues Resolved +............... + +See the `PyMongo 4.11.2 release notes in JIRA`_ for the list of resolved issues in this release. + +.. _PyMongo 4.11.2 release notes in JIRA: https://jira.mongodb.org/secure/ReleaseNote.jspa?projectId=10004&version=42506 + +Changes in Version 4.11.1 (2025/02/10) -------------------------------------- - Fixed support for prebuilt ``ppc64le`` and ``s390x`` wheels. diff --git a/pymongo/message.py b/pymongo/message.py index 10c9edb5cd..8e2fd6f990 100644 --- a/pymongo/message.py +++ b/pymongo/message.py @@ -105,7 +105,7 @@ "insert": "documents", "update": "updates", "delete": "deletes", - "bulkWrite": "bulkWrite", + "bulkWrite": "ops", } _UNICODE_REPLACE_CODEC_OPTIONS: CodecOptions[Mapping[str, Any]] = CodecOptions( diff --git a/test/asynchronous/test_database.py b/test/asynchronous/test_database.py index 55a8cc3ab2..2bbf763ab3 100644 --- a/test/asynchronous/test_database.py +++ b/test/asynchronous/test_database.py @@ -430,6 +430,21 @@ async def test_command_with_regex(self): for doc in result["cursor"]["firstBatch"]: self.assertTrue(isinstance(doc["r"], Regex)) + async def test_command_bulkWrite(self): + # Ensure bulk write commands can be run directly via db.command(). + if async_client_context.version.at_least(8, 0): + await self.client.admin.command( + { + "bulkWrite": 1, + "nsInfo": [{"ns": self.db.test.full_name}], + "ops": [{"insert": 0, "document": {}}], + } + ) + await self.db.command({"insert": "test", "documents": [{}]}) + await self.db.command({"update": "test", "updates": [{"q": {}, "u": {"$set": {"x": 1}}}]}) + await self.db.command({"delete": "test", "deletes": [{"q": {}, "limit": 1}]}) + await self.db.test.drop() + async def test_cursor_command(self): db = self.client.pymongo_test await db.test.drop() diff --git a/test/test_database.py b/test/test_database.py index aad9089bd8..48cca921b1 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -425,6 +425,21 @@ def test_command_with_regex(self): for doc in result["cursor"]["firstBatch"]: self.assertTrue(isinstance(doc["r"], Regex)) + def test_command_bulkWrite(self): + # Ensure bulk write commands can be run directly via db.command(). + if client_context.version.at_least(8, 0): + self.client.admin.command( + { + "bulkWrite": 1, + "nsInfo": [{"ns": self.db.test.full_name}], + "ops": [{"insert": 0, "document": {}}], + } + ) + self.db.command({"insert": "test", "documents": [{}]}) + self.db.command({"update": "test", "updates": [{"q": {}, "u": {"$set": {"x": 1}}}]}) + self.db.command({"delete": "test", "deletes": [{"q": {}, "limit": 1}]}) + self.db.test.drop() + def test_cursor_command(self): db = self.client.pymongo_test db.test.drop()