fix for pymongo < 3.7

This commit is contained in:
Bastien Gérard 2020-01-07 22:24:55 +01:00
parent e64a7a9448
commit 2fa48cd9e5

View File

@ -29,20 +29,22 @@ def count_documents(
if collation is not None: if collation is not None:
kwargs["collation"] = collation kwargs["collation"] = collation
try: # count_documents appeared in pymongo 3.7
return collection.count_documents(filter=filter, **kwargs) if IS_PYMONGO_GTE_37:
except (AttributeError, OperationFailure): try:
# AttributeError - count_documents appeared in pymongo 3.7 return collection.count_documents(filter=filter, **kwargs)
# OperationFailure - accounts for some operators that used to work except OperationFailure:
# with .count but are no longer working with count_documents (i.e $geoNear, $near, and $nearSphere) # OperationFailure - accounts for some operators that used to work
# fallback to deprecated Cursor.count # with .count but are no longer working with count_documents (i.e $geoNear, $near, and $nearSphere)
# Keeping this should be reevaluated the day pymongo removes .count entirely # fallback to deprecated Cursor.count
cursor = collection.find(filter) # Keeping this should be reevaluated the day pymongo removes .count entirely
for option, option_value in kwargs.items(): pass
cursor_method = getattr(cursor, option)
cursor = cursor_method(option_value) cursor = collection.find(filter)
count = cursor.count() for option, option_value in kwargs.items():
return count cursor_method = getattr(cursor, option)
cursor = cursor_method(option_value)
return cursor.count()
def list_collection_names(db, include_system_collections=False): def list_collection_names(db, include_system_collections=False):