Skip to content

Commit 8075dad

Browse files
committed
PYTHON-4915 - Add guidance on adding _id fields to documents to CRUD spec
1 parent 7286386 commit 8075dad

File tree

8 files changed

+40
-8
lines changed

8 files changed

+40
-8
lines changed

pymongo/asynchronous/bulk.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ def add_insert(self, 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()} | document # type: ignore[index]
137+
document.clear()
138+
document.update(new_document)
137139
self.ops.append((_INSERT, document))
138140

139141
def add_update(

pymongo/asynchronous/client_bulk.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ 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()} | document # type: ignore[index]
137+
document.clear()
138+
document.update(new_document)
137139
cmd = {"insert": -1, "document": document}
138140
self.ops.append(("insert", cmd))
139141
self.namespaces.append(namespace)

pymongo/asynchronous/collection.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,9 @@ async def insert_one(
885885
"""
886886
common.validate_is_document_type("document", document)
887887
if not (isinstance(document, RawBSONDocument) or "_id" in document):
888-
document["_id"] = ObjectId() # type: ignore[index]
888+
new_document = {"_id": ObjectId()} | document # type: ignore[index]
889+
document.clear()
890+
document.update(new_document)
889891

890892
write_concern = self._write_concern_for(session)
891893
return InsertOneResult(
@@ -966,7 +968,9 @@ def gen() -> Iterator[tuple[int, Mapping[str, Any]]]:
966968
common.validate_is_document_type("document", document)
967969
if not isinstance(document, RawBSONDocument):
968970
if "_id" not in document:
969-
document["_id"] = ObjectId() # type: ignore[index]
971+
new_document = {"_id": ObjectId()} | document # type: ignore[index]
972+
document.clear()
973+
document.update(new_document)
970974
inserted_ids.append(document["_id"])
971975
yield (message._INSERT, document)
972976

pymongo/synchronous/bulk.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ def add_insert(self, 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()} | document # type: ignore[index]
137+
document.clear()
138+
document.update(new_document)
137139
self.ops.append((_INSERT, document))
138140

139141
def add_update(

pymongo/synchronous/client_bulk.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ 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()} | document # type: ignore[index]
137+
document.clear()
138+
document.update(new_document)
137139
cmd = {"insert": -1, "document": document}
138140
self.ops.append(("insert", cmd))
139141
self.namespaces.append(namespace)

pymongo/synchronous/collection.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,9 @@ def insert_one(
884884
"""
885885
common.validate_is_document_type("document", document)
886886
if not (isinstance(document, RawBSONDocument) or "_id" in document):
887-
document["_id"] = ObjectId() # type: ignore[index]
887+
new_document = {"_id": ObjectId()} | document # type: ignore[index]
888+
document.clear()
889+
document.update(new_document)
888890

889891
write_concern = self._write_concern_for(session)
890892
return InsertOneResult(
@@ -965,7 +967,9 @@ def gen() -> Iterator[tuple[int, Mapping[str, Any]]]:
965967
common.validate_is_document_type("document", document)
966968
if not isinstance(document, RawBSONDocument):
967969
if "_id" not in document:
968-
document["_id"] = ObjectId() # type: ignore[index]
970+
new_document = {"_id": ObjectId()} | document # type: ignore[index]
971+
document.clear()
972+
document.update(new_document)
969973
inserted_ids.append(document["_id"])
970974
yield (message._INSERT, document)
971975

test/asynchronous/test_collection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,6 +2277,14 @@ async def afind(*args, **kwargs):
22772277
for helper, args in helpers:
22782278
await helper(*args, let={}) # type: ignore
22792279

2280+
async def test_generated_id_order(self):
2281+
listener = OvertCommandListener()
2282+
c = await self.async_single_client(event_listeners=[listener])
2283+
listener.reset()
2284+
2285+
await c.db.test.insert_one({"x": 1})
2286+
self.assertEqual("_id", next(iter(listener.started_events[0].command["documents"][0])))
2287+
22802288

22812289
if __name__ == "__main__":
22822290
unittest.main()

test/test_collection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,14 @@ def afind(*args, **kwargs):
22542254
for helper, args in helpers:
22552255
helper(*args, let={}) # type: ignore
22562256

2257+
def test_generated_id_order(self):
2258+
listener = OvertCommandListener()
2259+
c = self.single_client(event_listeners=[listener])
2260+
listener.reset()
2261+
2262+
c.db.test.insert_one({"x": 1})
2263+
self.assertEqual("_id", next(iter(listener.started_events[0].command["documents"][0])))
2264+
22572265

22582266
if __name__ == "__main__":
22592267
unittest.main()

0 commit comments

Comments
 (0)