From 2fa48cd9e5207ad753419ace20dc0c615c7481cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastien=20G=C3=A9rard?= Date: Tue, 7 Jan 2020 22:24:55 +0100 Subject: [PATCH] fix for pymongo < 3.7 --- mongoengine/pymongo_support.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/mongoengine/pymongo_support.py b/mongoengine/pymongo_support.py index 3aef4e09..284efc2f 100644 --- a/mongoengine/pymongo_support.py +++ b/mongoengine/pymongo_support.py @@ -29,20 +29,22 @@ def count_documents( if collation is not None: kwargs["collation"] = collation - try: - return collection.count_documents(filter=filter, **kwargs) - except (AttributeError, OperationFailure): - # AttributeError - count_documents appeared in pymongo 3.7 - # OperationFailure - accounts for some operators that used to work - # with .count but are no longer working with count_documents (i.e $geoNear, $near, and $nearSphere) - # fallback to deprecated Cursor.count - # Keeping this should be reevaluated the day pymongo removes .count entirely - cursor = collection.find(filter) - for option, option_value in kwargs.items(): - cursor_method = getattr(cursor, option) - cursor = cursor_method(option_value) - count = cursor.count() - return count + # count_documents appeared in pymongo 3.7 + if IS_PYMONGO_GTE_37: + try: + return collection.count_documents(filter=filter, **kwargs) + except OperationFailure: + # OperationFailure - accounts for some operators that used to work + # with .count but are no longer working with count_documents (i.e $geoNear, $near, and $nearSphere) + # fallback to deprecated Cursor.count + # Keeping this should be reevaluated the day pymongo removes .count entirely + pass + + cursor = collection.find(filter) + for option, option_value in kwargs.items(): + cursor_method = getattr(cursor, option) + cursor = cursor_method(option_value) + return cursor.count() def list_collection_names(db, include_system_collections=False):