summaryrefslogtreecommitdiff
path: root/requests_cache/backends/mongodb.py
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook.git@proton.me>2022-10-24 15:08:00 -0500
committerJordan Cook <jordan.cook.git@proton.me>2023-03-01 17:36:27 -0600
commit6d69d5f81da7f7c0323dd858e534597a65a9053c (patch)
tree5ac4658941134d00eac0d99f3cb2980274ad6a06 /requests_cache/backends/mongodb.py
parent812301ab85a3b54387dcebd7d65407ace2589b9f (diff)
downloadrequests-cache-mongita.tar.gz
WIP: Minor workaround in MongoDB backend to support mongitamongita
Diffstat (limited to 'requests_cache/backends/mongodb.py')
-rw-r--r--requests_cache/backends/mongodb.py12
1 files changed, 8 insertions, 4 deletions
diff --git a/requests_cache/backends/mongodb.py b/requests_cache/backends/mongodb.py
index 76b33be..c240eb1 100644
--- a/requests_cache/backends/mongodb.py
+++ b/requests_cache/backends/mongodb.py
@@ -129,18 +129,22 @@ class MongoDict(BaseStorage):
value = self.serialize(value)
if not isinstance(value, Mapping):
value = {'data': value}
- self.collection.replace_one({'_id': key}, value, upsert=True)
+ self.collection.replace_one({'_id': key}, replacement=value, upsert=True)
def __delitem__(self, key):
- result = self.collection.find_one_and_delete({'_id': key}, {'_id': True})
+ result = self.collection.find_one_and_delete({'_id': key}, projection={'_id': True})
if result is None:
raise KeyError
def __len__(self):
- return self.collection.estimated_document_count()
+ # First attempt a fast estimated count, then fallback to full count (compat with mongita)
+ try:
+ return self.collection.estimated_document_count()
+ except NotImplementedError:
+ return self.collection.count_documents({})
def __iter__(self):
- for d in self.collection.find({}, {'_id': True}):
+ for d in self.collection.find({}, projection={'_id': True}):
yield d['_id']
def bulk_delete(self, keys: Iterable[str]):