Add date filter stub
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
from pymongo import MongoClient
|
||||
from pymongo.synchronous.database import Database
|
||||
@@ -27,13 +28,31 @@ class MongoService:
|
||||
except Exception as e:
|
||||
logger.log_info(e)
|
||||
|
||||
def find(self, collection: str, field: str, value):
|
||||
def find_by_value(self, collection: str, field: str, value):
|
||||
try :
|
||||
collection = self._db[collection]
|
||||
return list(collection.find({field: value}))
|
||||
except Exception as e:
|
||||
logger.log_info(e)
|
||||
|
||||
def find_by_date(self, collection: str, filters: dict = None, timestamp_field: str = "created_at",
|
||||
start_time: datetime = None, end_time: datetime = None):
|
||||
try:
|
||||
collection = self._db[collection]
|
||||
query = filters.copy() if filters else {}
|
||||
|
||||
if start_time or end_time:
|
||||
query[timestamp_field] = {}
|
||||
if start_time:
|
||||
query[timestamp_field]["$gte"] = start_time
|
||||
if end_time:
|
||||
query[timestamp_field]["$lte"] = end_time
|
||||
|
||||
return list(collection.find(query))
|
||||
except Exception as e:
|
||||
logger.log_info(e)
|
||||
return []
|
||||
|
||||
def _to_document(self, obj):
|
||||
if obj is None or isinstance(obj, (str, int, float, bool)):
|
||||
return obj
|
||||
|
||||
Reference in New Issue
Block a user