Merge pull request #2166 from bagerard/add_aggregate_tutorial
Document Model.objects.aggregate entrypoint with an example
This commit is contained in:
commit
02a920feea
@ -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
|
You could technically use ``len(User.objects)`` to get the same result, but it
|
||||||
would be significantly slower than :meth:`~mongoengine.queryset.QuerySet.count`.
|
would be significantly slower than :meth:`~mongoengine.queryset.QuerySet.count`.
|
||||||
When you execute a server-side count query, you let MongoDB do the heavy
|
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
|
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`.
|
than :meth:`~mongoengine.queryset.QuerySet.count`.
|
||||||
|
|
||||||
Further aggregation
|
Further aggregation
|
||||||
@ -386,6 +386,25 @@ would be generating "tag-clouds"::
|
|||||||
top_tags = sorted(tag_freqs.items(), key=itemgetter(1), reverse=True)[:10]
|
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 <https://api.mongodb.com/python/current/examples/aggregation.html#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
|
Query efficiency and performance
|
||||||
================================
|
================================
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user