Skip to content

Commit 425cd1b

Browse files
committed
Reorder client.bulkWrite _id
1 parent 8906e84 commit 425cd1b

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

pymongo/asynchronous/client_bulk.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ def add_insert(self, namespace: str, document: _DocumentOut) -> None:
133133
validate_is_document_type("document", document)
134134
# Generate ObjectId client side.
135135
if not (isinstance(document, RawBSONDocument) or "_id" in document):
136-
document["_id"] = ObjectId()
136+
new_document = {"_id": ObjectId()}
137+
new_document.update(document)
138+
document.clear()
139+
document.update(new_document)
137140
cmd = {"insert": -1, "document": document}
138141
self.ops.append(("insert", cmd))
139142
self.namespaces.append(namespace)

pymongo/synchronous/client_bulk.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ def add_insert(self, namespace: str, document: _DocumentOut) -> None:
133133
validate_is_document_type("document", document)
134134
# Generate ObjectId client side.
135135
if not (isinstance(document, RawBSONDocument) or "_id" in document):
136-
document["_id"] = ObjectId()
136+
new_document = {"_id": ObjectId()}
137+
new_document.update(document)
138+
document.clear()
139+
document.update(new_document)
137140
cmd = {"insert": -1, "document": document}
138141
self.ops.append(("insert", cmd))
139142
self.namespaces.append(namespace)

test/mockupdb/test_id_ordering.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
import pytest
66

7+
from pymongo import InsertOne
8+
79
try:
8-
from mockupdb import MockupDB, OpMsg, going
10+
from mockupdb import MockupDB, OpMsg, go, going
911

1012
_HAVE_MOCKUPDB = True
1113
except ImportError:
@@ -25,7 +27,7 @@ def test_id_ordering(self):
2527
isWritablePrimary=True,
2628
msg="isdbgrid",
2729
minWireVersion=0,
28-
maxWireVersion=20,
30+
maxWireVersion=25,
2931
helloOk=True,
3032
serviceId=ObjectId(),
3133
)
@@ -35,6 +37,16 @@ def test_id_ordering(self):
3537
client = self.simple_client(server.uri, loadBalanced=True)
3638
collection = client.db.coll
3739
with going(collection.insert_one, {"x": 1}):
38-
request = server.receives(OpMsg({"insert": "coll"}))
40+
request = server.receives()
41+
self.assertEqual("_id", next(iter(request["documents"][0])))
42+
request.reply({"ok": 1})
43+
44+
with going(collection.bulk_write, [InsertOne({"x1": 1})]):
45+
request = server.receives()
3946
self.assertEqual("_id", next(iter(request["documents"][0])))
4047
request.reply({"ok": 1})
48+
49+
with going(client.bulk_write, [InsertOne(namespace="db.coll", document={"x2": 1})]):
50+
request = server.receives()
51+
self.assertEqual("_id", next(iter(request["ops"][0]["document"])))
52+
request.reply({"ok": 1})

0 commit comments

Comments
 (0)