Skip to content

Commit edbaa18

Browse files
committed
PYTHON-4991 Fix perf client.bulkWrite perf regression
1 parent 493fc2a commit edbaa18

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

pymongo/message.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import datetime
2525
import random
2626
import struct
27-
from collections import ChainMap
2827
from io import BytesIO as _BytesIO
2928
from typing import (
3029
TYPE_CHECKING,
@@ -1117,14 +1116,14 @@ def _check_doc_size_limits(
11171116
op_doc[op_type] = ns_info[namespace] # type: ignore[index]
11181117

11191118
# Since the data document itself is nested within the insert document
1120-
# it won't be automatically re-ordered by the BSON conversion.
1121-
# We use ChainMap here to make the _id field the first field instead.
1119+
# it won't be automatically re-ordered by the BSON conversion, so we need
1120+
# to encode it directly as a top-level document first.
11221121
doc_to_encode = op_doc
11231122
if real_op_type == "insert":
11241123
doc = op_doc["document"]
11251124
if not isinstance(doc, RawBSONDocument):
11261125
doc_to_encode = op_doc.copy() # type: ignore[attr-defined] # Shallow copy
1127-
doc_to_encode["document"] = ChainMap(doc, {"_id": doc["_id"]}) # type: ignore[index]
1126+
doc_to_encode["document"] = RawBSONDocument(_dict_to_bson(doc, False, opts)) # type: ignore[index]
11281127

11291128
# Encode current operation doc and, if newly added, namespace doc.
11301129
op_doc_encoded = _dict_to_bson(doc_to_encode, False, opts)

0 commit comments

Comments
 (0)