Skip to content

Commit e35dab5

Browse files
committed
Update with Aggregation Pipeline doc
1 parent c03af4d commit e35dab5

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

docs/guide/querying.rst

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,35 @@ However, this doesn't map well to the syntax so you can also use a capital S ins
218218

219219
Raw queries
220220
-----------
221-
It is possible to provide a raw :mod:`PyMongo` query as a query parameter, which will
222-
be integrated directly into the query. This is done using the ``__raw__``
221+
It is possible to provide a raw :mod:`PyMongo` query as a query parameter or update as a update parameter , which will
222+
be integrated directly into the query or update. This is done using the ``__raw__``
223223
keyword argument::
224224

225225
Page.objects(__raw__={'tags': 'coding'})
226226

227+
# or for update
228+
229+
Page.objects(__raw__={'tags': 'coding'}).update(__raw__={'$set': {'tags': 'coding'}})
230+
231+
Page.objects(tags='coding').update(__raw__={'$set': {'tags': 'coding'}})
232+
233+
.. versionadded:: 0.4
234+
235+
236+
Update with Aggregation Pipeline
237+
-----------
238+
It is possible to provide a raw :mod:`PyMongo` aggregation update parameter, which will
239+
be integrated directly into the update. This is done using the ``aggregation_update=True`` and ``__raw__``
240+
keyword argument::
241+
242+
243+
# 'tags' field is set to 'coding is fun'
244+
Page.objects(tags='coding').update(__raw__=[
245+
{"$set": {"tags": {"$concat": ["$tags", "is fun"]}}}
246+
],
247+
aggregation_update=True,
248+
)
249+
227250
.. versionadded:: 0.4
228251

229252
Sorting/Ordering results

mongoengine/queryset/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,10 @@ def update(
554554
queryset = self.clone()
555555
query = queryset._query
556556
if aggregation_update:
557+
if "__raw__" not in update:
558+
raise OperationError(
559+
"Currently aggregation_update works only with __raw__ value"
560+
)
557561
update = [
558562
transform.update(queryset._document, **{"__raw__": u})
559563
for u in update["__raw__"]

0 commit comments

Comments
 (0)