From a70ef5594d5e0ac378c8d718dc5bca2dfeeca7f3 Mon Sep 17 00:00:00 2001 From: marcoskv Date: Sun, 26 Jul 2015 22:41:24 +0200 Subject: [PATCH] QuerySet count vs len #937 Current documentation does not consider performance issues in using len instead of count. https://github.com/MongoEngine/mongoengine/issues/937 --- docs/guide/querying.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/guide/querying.rst b/docs/guide/querying.rst index f2e71b04..5f7a3de9 100644 --- a/docs/guide/querying.rst +++ b/docs/guide/querying.rst @@ -347,6 +347,8 @@ way of achieving this:: num_users = len(User.objects) +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`. + Further aggregation ------------------- You may sum over the values of a specific field on documents using