From 19ef2be88b65d5f2103fded394b3c9b0b9b6973b Mon Sep 17 00:00:00 2001 From: Stefan Wojcik Date: Sun, 5 Mar 2017 00:05:33 -0500 Subject: [PATCH] fix #937 --- docs/guide/querying.rst | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/docs/guide/querying.rst b/docs/guide/querying.rst index 980947df..0bb19658 100644 --- a/docs/guide/querying.rst +++ b/docs/guide/querying.rst @@ -340,14 +340,19 @@ Javascript code that is executed on the database server. Counting results ---------------- -Just as with limiting and skipping results, there is a method on -:class:`~mongoengine.queryset.QuerySet` objects -- -:meth:`~mongoengine.queryset.QuerySet.count`, but there is also a more Pythonic -way of achieving this:: +Just as with limiting and skipping results, there is a method on a +:class:`~mongoengine.queryset.QuerySet` object -- +:meth:`~mongoengine.queryset.QuerySet.count`:: - num_users = len(User.objects) + num_users = User.objects.count() -Even if len() is the Pythonic way of counting results, keep in mind that if you concerned about performance, :meth:`~mongoengine.queryset.QuerySet.count` is the way to go since it only execute a server side count query, while len() retrieves the results, places them in cache, and finally counts them. If we compare the performance of the two operations, len() is much slower than :meth:`~mongoengine.queryset.QuerySet.count`. +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() +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 +than :meth:`~mongoengine.queryset.QuerySet.count`. Further aggregation -------------------