-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.py
209 lines (154 loc) · 6.91 KB
/
mongo.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import collections
"""
A helper file for using mongo db
Class document aims to make using mongo calls easy, saves
needing to know the syntax for it. Just pass in the db instance
on init and the document to create an instance on and boom
"""
class Document:
_version = 3 # A flag for use during help
def __init__(self, connection, document_name):
self.db = connection[document_name]
# <-- Pointer Methods -->
async def find(self, data_id):
return await self.find_by_id(data_id)
async def delete(self, data_id):
await self.delete_by_id(data_id)
# <-- Actual Methods -->
async def update(self, data, *args, **kwargs):
await self.update_by_id(data, *args, **kwargs)
async def get_all(self, filter_dict=None, *args, **kwargs):
if filter_dict is None:
filter_dict = {}
return await self.db.find(filter_dict, *args, **kwargs).to_list(None)
async def find_by_id(self, data_id):
return await self.db.find_one({"id": data_id})
async def find_by_custom(self, filter_dict):
self.__ensure_dict(filter_dict)
return await self.db.find_one(filter_dict)
async def find_many_by_custom(self, filter_dict):
self.__ensure_dict(filter_dict)
return await self.db.find(filter_dict).to_list(None)
async def delete_by_id(self, data_id):
if await self.find_by_id(data_id) is None:
return
await self.db.delete_many({"id": data_id})
async def delete_by_custom(self, filter_dict):
self.__ensure_dict(filter_dict)
if await self.find_by_custom(filter_dict) is None:
return
# TODO Discuss this in ep 27.3
return await self.db.delete_many(filter_dict)
async def insert(self, data):
self.__ensure_dict(data)
await self.db.insert_one(data)
async def upsert(self, data, option="set", *args, **kwargs):
await self.update_by_id(data, option, upsert=True, *args, **kwargs)
async def update_by_id(self, data, option="set", *args, **kwargs):
self.__ensure_dict(data)
self.__ensure_id(data)
if await self.find_by_id(data["id"]) is None:
return await self.insert(data)
data_id = data.pop("id")
await self.db.update_one(
{"id": data_id}, {f"${option}": data}, *args, **kwargs
)
async def upsert_custom(
self, filter_dict, update_data, option="set", *args, **kwargs
):
await self.update_by_custom(
filter_dict, update_data, option, upsert=True, *args, **kwargs
)
async def update_by_custom(
self, filter_dict, update_data, option="set", *args, **kwargs
):
self.__ensure_dict(filter_dict)
self.__ensure_dict(update_data)
if not bool(await self.find_by_custom(filter_dict)):
# Insert
return await self.insert({**filter_dict, **update_data})
# Update
await self.db.update_one(
filter_dict, {f"${option}": update_data}, *args, **kwargs
)
async def unset(self, data):
self.__ensure_dict(data)
self.__ensure_id(data)
if await self.find_by_id(data["id"]) is None:
return
data_id = data.pop("id")
await self.db.update_one({"id": data_id}, {"$unset": data})
async def increment(self, data_id, amount, field):
if await self.find_by_id(data_id) is None:
return
await self.db.update_one({"id": data_id}, {"$inc": {field: amount}})
# <-- Private methods -->
async def __get_raw(self, data_id):
return await self.db.find_one({"id": data_id})
#----------------------------------------------------------------------------------------------------------------------------------------
# MY OWN DEFS
#----------------------------------------------------------------------------------------------------------------------------------------
async def find_document(self, id, document):
key = {"id": id}
server= await self.db.find_one(key)
document= server[document]
return document
async def find_field(self, id, document, field):
key = {"id": id}
server= await self.db.find_one(key)
document= server[document]
field = document[field]
return field
async def find_field_value(self, id, document, field, field_value):
key = {"id": id}
server= await self.db.find_one(key)
document= server[document]
field = document[field]
field_value= field[field_value]
return field_value
async def find_field_value_value(self, id, document, field, field_value, value):
key = {"id": id}
server= await self.db.find_one(key)
document= server[document]
field = document[field]
field_value= field[field_value]
value= field_value[value]
return value
#----------------------------------------------------------------------------------------------------------------------------------------
async def update_dc(self, id, document):
key = {"id": id}
await self.db.update_one(key, {'$set':document})
async def update_document(self, id, document,value):
key = {"id": id}
await self.db.update_one(key, {'$set':{document:value}})
async def update_field(self, id, document, field,value):
key = {"id": id}
await self.db.update_one(key, {'$set':{f'{document}.{field}': value}})
async def update_field_value(self, id, document, field, field_value, value):
key = {"id": id}
await self.db.update_one(key, {'$set':{f'{document}.{field}.{field_value}': value}})
async def update_field_value_value(self, id, document, field, field_value, value,value2):
key = {"id": id}
await self.db.update_one(key, {'$set':{f'{document}.{field}.{field_value}.{value}': value2}})
async def push_field(self, id, document, field, value):
key = {'id': id}
await self.db.update_one(key, {'$push':{f'{document}.{field}': value}})
#----------------------------------------------------------------------------------------------------------------------------------------
async def delete_db(self, id):
key = {"id": id}
await self.db.delete_one(key)
async def delete_document(self, id, document):
key = {"id": id}
await self.db.update_one(key, {'$unset':{document:''}})
async def delete_field(self, id, document, field):
key = {"id": id}
await self.db.update_one(key, {'$unset':{f'{document}.{field}':''}})
async def delete_field_value(self, id, document, field,value):
key = {"id": id}
await self.db.update_one(key, {'$unset':{f'{document}.{field}.{value}':''}})
@staticmethod
def __ensure_dict(data):
assert isinstance(data, collections.abc.Mapping)
@staticmethod
def __ensure_id(data):
assert "_id" in data