From 6d69d5f81da7f7c0323dd858e534597a65a9053c Mon Sep 17 00:00:00 2001 From: Jordan Cook Date: Mon, 24 Oct 2022 15:08:00 -0500 Subject: WIP: Minor workaround in MongoDB backend to support mongita --- requests_cache/backends/mongodb.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'requests_cache/backends/mongodb.py') 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]): -- cgit v1.2.1