-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongo_helper.py
More file actions
64 lines (45 loc) · 1.89 KB
/
Copy pathmongo_helper.py
File metadata and controls
64 lines (45 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import motor.motor_asyncio
class MongoHelper:
def __init__(self, db_name, collection_name):
self.db_name = db_name
self.collection_name = collection_name
self.client = self.get_client
self.db = self.get_db()
self.collection = self.get_collection()
@property
def get_client(self):
return motor.motor_asyncio.AsyncIOMotorClient(os.getenv('DB_URI'))
def get_db(self):
return self.client[self.db_name]
def get_collection(self):
return self.db[self.collection_name]
async def insert_one(self, data):
return await self.collection.insert_one(data)
async def insert_many(self, data):
return await self.collection.insert_many(data)
async def find_one(self, query):
return await self.collection.find_one(query)
async def find_many(self, query):
cursor = self.collection.find(query)
return await cursor.to_list(length=None)
async def delete_one(self, query):
return await self.collection.delete_one(query)
async def delete_many(self, query):
return await self.collection.delete_many(query)
async def update_one(self, query, data):
return await self.collection.update_one(query, data)
async def update_many(self, query, data):
return await self.collection.update_many(query, data)
async def count(self, query):
return await self.collection.count(query)
async def distinct(self, field, query):
return await self.collection.distinct(field, query)
async def aggregate(self, query):
cursor = self.collection.aggregate(query)
return await cursor.to_list(length=None)
async def count_documents(self, query):
return await self.collection.count_documents(query)
async def find_all(self):
cursor = self.collection.find()
return await cursor.to_list(length=None)