Skip to content

Commit ad88879

Browse files
PYTHON-4666 Fix handling of large documents in client.bulk_write (mongodb#1798)
1 parent f16206c commit ad88879

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

pymongo/message.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,8 @@ def _check_doc_size_limits(
11231123
new_message_size = total_ops_length + total_ns_length + op_length + ns_length
11241124
# We have enough data, return this batch.
11251125
if new_message_size > max_doc_sequences_bytes:
1126+
if idx == 0:
1127+
_raise_document_too_large(op_type, op_length, max_bson_size + _COMMAND_OVERHEAD)
11261128
break
11271129

11281130
# Add op and ns documents to this batch.

test/asynchronous/test_client_bulk_write.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,17 +512,15 @@ async def test_returns_error_if_no_writes_can_be_added_to_ops(self):
512512
# Document too large.
513513
b_repeated = "b" * self.max_message_size_bytes
514514
models = [InsertOne(namespace="db.coll", document={"a": b_repeated})]
515-
with self.assertRaises(InvalidOperation) as context:
515+
with self.assertRaises(DocumentTooLarge):
516516
await client.bulk_write(models=models)
517-
self.assertIn("cannot do an empty bulk write", context.exception._message)
518517

519518
# Namespace too large.
520519
c_repeated = "c" * self.max_message_size_bytes
521520
namespace = f"db.{c_repeated}"
522521
models = [InsertOne(namespace=namespace, document={"a": "b"})]
523-
with self.assertRaises(InvalidOperation) as context:
522+
with self.assertRaises(DocumentTooLarge):
524523
await client.bulk_write(models=models)
525-
self.assertIn("cannot do an empty bulk write", context.exception._message)
526524

527525
@async_client_context.require_version_min(8, 0, 0, -24)
528526
@unittest.skipUnless(_HAVE_PYMONGOCRYPT, "pymongocrypt is not installed")

test/test_client_bulk_write.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,17 +512,15 @@ def test_returns_error_if_no_writes_can_be_added_to_ops(self):
512512
# Document too large.
513513
b_repeated = "b" * self.max_message_size_bytes
514514
models = [InsertOne(namespace="db.coll", document={"a": b_repeated})]
515-
with self.assertRaises(InvalidOperation) as context:
515+
with self.assertRaises(DocumentTooLarge):
516516
client.bulk_write(models=models)
517-
self.assertIn("cannot do an empty bulk write", context.exception._message)
518517

519518
# Namespace too large.
520519
c_repeated = "c" * self.max_message_size_bytes
521520
namespace = f"db.{c_repeated}"
522521
models = [InsertOne(namespace=namespace, document={"a": "b"})]
523-
with self.assertRaises(InvalidOperation) as context:
522+
with self.assertRaises(DocumentTooLarge):
524523
client.bulk_write(models=models)
525-
self.assertIn("cannot do an empty bulk write", context.exception._message)
526524

527525
@client_context.require_version_min(8, 0, 0, -24)
528526
@unittest.skipUnless(_HAVE_PYMONGOCRYPT, "pymongocrypt is not installed")

0 commit comments

Comments
 (0)