Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mongoengine/queryset/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,14 @@ def order_by(self, *keys):
queryset._ordering = queryset._get_order_by(keys)
return queryset

def comment(self, text):
"""Add a comment to the query.

See https://docs.mongodb.com/manual/reference/method/cursor.comment/#cursor.comment
for details.
"""
return self._chainable_method("comment", text)

def explain(self, format=False):
"""Return an explain plan record for the
:class:`~mongoengine.queryset.QuerySet`\ 's cursor.
Expand Down
16 changes: 15 additions & 1 deletion tests/queryset/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ class B(Document):

def test_update_write_concern(self):
"""Test that passing write_concern works"""

self.Person.drop_collection()

write_concern = {"fsync": True}
Expand Down Expand Up @@ -2199,6 +2198,21 @@ class Author(Document):
a.author.name for a in Author.objects.order_by('-author__age')]
self.assertEqual(names, ['User A', 'User B', 'User C'])

def test_comment(self):
"""Make sure adding a comment to the query works."""
class User(Document):
age = IntField()

with db_ops_tracker() as q:
adult = (User.objects.filter(age__gte=18)
.comment('looking for an adult')
.first())
ops = q.get_ops()
self.assertEqual(len(ops), 1)
op = ops[0]
self.assertEqual(op['query']['$query'], {'age': {'$gte': 18}})
self.assertEqual(op['query']['$comment'], 'looking for an adult')

def test_map_reduce(self):
"""Ensure map/reduce is both mapping and reducing.
"""
Expand Down