Skip to content

PYTHON-5166 Allow Database.command to run bulkWrite commands (#2164) [v4.11] #2172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pymongo/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"insert": "documents",
"update": "updates",
"delete": "deletes",
"bulkWrite": "bulkWrite",
"bulkWrite": "ops",
}

_UNICODE_REPLACE_CODEC_OPTIONS: CodecOptions[Mapping[str, Any]] = CodecOptions(
Expand Down
15 changes: 15 additions & 0 deletions test/asynchronous/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions test/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading