diff --git a/docs/guide/querying.rst b/docs/guide/querying.rst index 151855a6..d64c169c 100644 --- a/docs/guide/querying.rst +++ b/docs/guide/querying.rst @@ -349,9 +349,9 @@ Just as with limiting and skipping results, there is a method on a You could technically use ``len(User.objects)`` to get the same result, but it would be significantly slower than :meth:`~mongoengine.queryset.QuerySet.count`. When you execute a server-side count query, you let MongoDB do the heavy -lifting and you receive a single integer over the wire. Meanwhile, len() +lifting and you receive a single integer over the wire. Meanwhile, ``len()`` retrieves all the results, places them in a local cache, and finally counts -them. If we compare the performance of the two operations, len() is much slower +them. If we compare the performance of the two operations, ``len()`` is much slower than :meth:`~mongoengine.queryset.QuerySet.count`. Further aggregation @@ -386,6 +386,25 @@ would be generating "tag-clouds":: top_tags = sorted(tag_freqs.items(), key=itemgetter(1), reverse=True)[:10] +MongoDB aggregation API +----------------------- +If you need to run aggregation pipelines, MongoEngine provides an entry point `Pymongo's aggregation framework `_ +through :meth:`~mongoengine.queryset.QuerySet.aggregate`. Check out Pymongo's documentation for the syntax and pipeline. +An example of its use would be:: + + class Person(Document): + name = StringField() + + Person(name='John').save() + Person(name='Bob').save() + + pipeline = [ + {"$sort" : {"name" : -1}}, + {"$project": {"_id": 0, "name": {"$toUpper": "$name"}}} + ] + data = Person.objects().aggregate(*pipeline) + assert data == [{'name': 'BOB'}, {'name': 'JOHN'}] + Query efficiency and performance ================================