Skip to content

Commit 315f3cd

Browse files
authored
PYTHON-5166 Allow Database.command to run bulkWrite commands (#2164) [v4.11] (#2172)
1 parent 736aa56 commit 315f3cd

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

doc/changelog.rst

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
Changelog
22
=========
33

4-
Changes in Version 4.11.1 (2025/MM/DD)
4+
Changes in Version 4.11.2 (YYYY/MM/DD)
5+
--------------------------------------
6+
7+
Version 4.11.2 is a bug fix release.
8+
9+
- Fixed a bug where :meth:`~pymongo.database.Database.command` would fail when attempting to run the bulkWrite command.
10+
11+
Issues Resolved
12+
...............
13+
14+
See the `PyMongo 4.11.2 release notes in JIRA`_ for the list of resolved issues in this release.
15+
16+
.. _PyMongo 4.11.2 release notes in JIRA: https://jira.mongodb.org/secure/ReleaseNote.jspa?projectId=10004&version=42506
17+
18+
Changes in Version 4.11.1 (2025/02/10)
519
--------------------------------------
620

721
- Fixed support for prebuilt ``ppc64le`` and ``s390x`` wheels.

pymongo/message.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
"insert": "documents",
106106
"update": "updates",
107107
"delete": "deletes",
108-
"bulkWrite": "bulkWrite",
108+
"bulkWrite": "ops",
109109
}
110110

111111
_UNICODE_REPLACE_CODEC_OPTIONS: CodecOptions[Mapping[str, Any]] = CodecOptions(

test/asynchronous/test_database.py

+15
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,21 @@ async def test_command_with_regex(self):
430430
for doc in result["cursor"]["firstBatch"]:
431431
self.assertTrue(isinstance(doc["r"], Regex))
432432

433+
async def test_command_bulkWrite(self):
434+
# Ensure bulk write commands can be run directly via db.command().
435+
if async_client_context.version.at_least(8, 0):
436+
await self.client.admin.command(
437+
{
438+
"bulkWrite": 1,
439+
"nsInfo": [{"ns": self.db.test.full_name}],
440+
"ops": [{"insert": 0, "document": {}}],
441+
}
442+
)
443+
await self.db.command({"insert": "test", "documents": [{}]})
444+
await self.db.command({"update": "test", "updates": [{"q": {}, "u": {"$set": {"x": 1}}}]})
445+
await self.db.command({"delete": "test", "deletes": [{"q": {}, "limit": 1}]})
446+
await self.db.test.drop()
447+
433448
async def test_cursor_command(self):
434449
db = self.client.pymongo_test
435450
await db.test.drop()

test/test_database.py

+15
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,21 @@ def test_command_with_regex(self):
425425
for doc in result["cursor"]["firstBatch"]:
426426
self.assertTrue(isinstance(doc["r"], Regex))
427427

428+
def test_command_bulkWrite(self):
429+
# Ensure bulk write commands can be run directly via db.command().
430+
if client_context.version.at_least(8, 0):
431+
self.client.admin.command(
432+
{
433+
"bulkWrite": 1,
434+
"nsInfo": [{"ns": self.db.test.full_name}],
435+
"ops": [{"insert": 0, "document": {}}],
436+
}
437+
)
438+
self.db.command({"insert": "test", "documents": [{}]})
439+
self.db.command({"update": "test", "updates": [{"q": {}, "u": {"$set": {"x": 1}}}]})
440+
self.db.command({"delete": "test", "deletes": [{"q": {}, "limit": 1}]})
441+
self.db.test.drop()
442+
428443
def test_cursor_command(self):
429444
db = self.client.pymongo_test
430445
db.test.drop()

0 commit comments

Comments
 (0)