Skip to content
Merged
5 changes: 4 additions & 1 deletion pymongo/asynchronous/client_bulk.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import copy
import datetime
import logging
from collections import ChainMap
from collections.abc import MutableMapping
from itertools import islice
from typing import (
Expand Down Expand Up @@ -133,7 +134,9 @@ def add_insert(self, namespace: str, document: _DocumentOut) -> None:
validate_is_document_type("document", document)
# Generate ObjectId client side.
if not (isinstance(document, RawBSONDocument) or "_id" in document):
document["_id"] = ObjectId()
document = ChainMap(document, {"_id": ObjectId()})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to add id to the input document here. Can you also add a test for that?

elif not isinstance(document, RawBSONDocument) and "_id" in document:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you refactor these two if statements to avoid repeating the checks?

if not isinstance(document, RawBSONDocument):
    if "_id" in document:
        ...
    else:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would also be worthwhile to add a comment explaining the ChainMap usage and why we have to use here but not in other insert code paths.

document = ChainMap(document, {"_id": document["_id"]})
cmd = {"insert": -1, "document": document}
self.ops.append(("insert", cmd))
self.namespaces.append(namespace)
Expand Down
5 changes: 4 additions & 1 deletion pymongo/synchronous/client_bulk.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import copy
import datetime
import logging
from collections import ChainMap
from collections.abc import MutableMapping
from itertools import islice
from typing import (
Expand Down Expand Up @@ -133,7 +134,9 @@ def add_insert(self, namespace: str, document: _DocumentOut) -> None:
validate_is_document_type("document", document)
# Generate ObjectId client side.
if not (isinstance(document, RawBSONDocument) or "_id" in document):
document["_id"] = ObjectId()
document = ChainMap(document, {"_id": ObjectId()})
elif not isinstance(document, RawBSONDocument) and "_id" in document:
document = ChainMap(document, {"_id": document["_id"]})
cmd = {"insert": -1, "document": document}
self.ops.append(("insert", cmd))
self.namespaces.append(namespace)
Expand Down
84 changes: 84 additions & 0 deletions test/mockupdb/test_id_ordering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright 2024-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add the boilerplate License comment.


from test import PyMongoTestCase

import pytest

from pymongo import InsertOne

try:
from mockupdb import MockupDB, OpMsg, go, going

_HAVE_MOCKUPDB = True
except ImportError:
_HAVE_MOCKUPDB = False


from bson.objectid import ObjectId

pytestmark = pytest.mark.mockupdb


class TestIdOrdering(PyMongoTestCase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a link to the crud spec that describes this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the spec is merged, yes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the spec merged?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. added!

def test_id_ordering(self):
server = MockupDB()
server.autoresponds(
"hello",
isWritablePrimary=True,
msg="isdbgrid",
minWireVersion=0,
maxWireVersion=25,
helloOk=True,
serviceId=ObjectId(),
)
server.run()
self.addCleanup(server.stop)

client = self.simple_client(server.uri, loadBalanced=True)
collection = client.db.coll
with going(collection.insert_one, {"x": 1}):
request = server.receives()
self.assertEqual("_id", next(iter(request["documents"][0])))
request.reply({"ok": 1})

with going(collection.bulk_write, [InsertOne({"x1": 1})]):
request = server.receives()
self.assertEqual("_id", next(iter(request["documents"][0])))
request.reply({"ok": 1})

with going(client.bulk_write, [InsertOne(namespace="db.coll", document={"x2": 1})]):
request = server.receives()
self.assertEqual("_id", next(iter(request["ops"][0]["document"])))
request.reply({"ok": 1})

# Re-ordering user-supplied _id fields is not required by the spec, but PyMongo does it for performance reasons
with going(collection.insert_one, {"x": 1, "_id": 111}):
request = server.receives()
self.assertEqual("_id", next(iter(request["documents"][0])))
request.reply({"ok": 1})

with going(collection.bulk_write, [InsertOne({"x1": 1, "_id": 1111})]):
request = server.receives()
self.assertEqual("_id", next(iter(request["documents"][0])))
request.reply({"ok": 1})

with going(
client.bulk_write, [InsertOne(namespace="db.coll", document={"x2": 1, "_id": 11111})]
):
request = server.receives()
self.assertEqual("_id", next(iter(request["ops"][0]["document"])))
request.reply({"ok": 1})
Loading