From 5e18e18b85720a24866fa984da6377fb2e1fd12e Mon Sep 17 00:00:00 2001 From: Adler Au Date: Fri, 6 Sep 2024 19:40:00 +0800 Subject: [PATCH] propagate collation, hint, comment to PyMongo functions propagate collation, hint, comment settings to corresponding PyMongo functions in update, delete and aggregate --- mongoengine/queryset/base.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/mongoengine/queryset/base.py b/mongoengine/queryset/base.py index 049aff22b..964e302f7 100644 --- a/mongoengine/queryset/base.py +++ b/mongoengine/queryset/base.py @@ -519,8 +519,20 @@ def delete(self, write_concern=None, _from_doc_delete=False, cascade_refs=None): write_concern=write_concern, **{"pull_all__%s" % field_name: self} ) + kwargs = {} + if self._hint not in (-1, None): + kwargs["hint"] = self._hint + if self._collation: + kwargs["collation"] = self._collation + if self._comment: + kwargs["comment"] = self._comment + with set_write_concern(queryset._collection, write_concern) as collection: - result = collection.delete_many(queryset._query, session=_get_session()) + result = collection.delete_many( + queryset._query, + session=_get_session(), + **kwargs, + ) # If we're using an unack'd write concern, we don't really know how # many items have been deleted at this point, hence we only return @@ -582,6 +594,15 @@ def update( update["$set"]["_cls"] = queryset._document._class_name else: update["$set"] = {"_cls": queryset._document._class_name} + + kwargs = {} + if self._hint not in (-1, None): + kwargs["hint"] = self._hint + if self._collation: + kwargs["collation"] = self._collation + if self._comment: + kwargs["comment"] = self._comment + try: with set_read_write_concern( queryset._collection, write_concern, read_concern @@ -595,6 +616,7 @@ def update( upsert=upsert, array_filters=array_filters, session=_get_session(), + **kwargs, ) if full_result: return result @@ -1388,8 +1410,18 @@ def aggregate(self, pipeline, *suppl_pipeline, **kwargs): read_preference=self._read_preference, read_concern=self._read_concern ) + if self._hint not in (-1, None): + kwargs.setdefault("hint", self._hint) + if self._collation: + kwargs.setdefault("collation", self._collation) + if self._comment: + kwargs.setdefault("comment", self._comment) + return collection.aggregate( - final_pipeline, cursor={}, session=_get_session(), **kwargs + final_pipeline, + cursor={}, + session=_get_session(), + **kwargs, ) # JS functionality