-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMongoAPI.py
More file actions
51 lines (41 loc) · 1.66 KB
/
MongoAPI.py
File metadata and controls
51 lines (41 loc) · 1.66 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
from flask import Flask, request, json, Response
from pymongo import MongoClient
import logging as log
class MongoAPI:
def __init__(self, data):
#self.client = MongoClient("mongodb://localhost:5000/")
self.client = MongoClient("mongodb://mymongo_1:27017/")
database = data['database']
collection = data['collection']
cursor = self.client[database]
self.collection = cursor[collection]
self.data = data
def read(self):
documents = self.collection.find()
output = [{item: data[item] for item in data if item != '_id'} for data in documents]
return output
def write(self, data):
log.info('Writing Data')
new_document = data['Document']
response = self.collection.insert_one(new_document)
output = {'Status': 'Successfully Inserted',
'Document_ID': str(response.inserted_id)}
return output
def update(self):
filt = self.data['Filter']
updated_data = {"$set": self.data['DataToBeUpdated']}
response = self.collection.update_one(filt, updated_data)
output = {'Status': 'Successfully Updated' if response.modified_count > 0 else "Nothing was updated."}
return output
def delete(self, data):
filt = data['Filter']
response = self.collection.delete_one(filt)
output = {'Status': 'Successfully Deleted' if response.deleted_count > 0 else "Document not found."}
return output
if __name__ == '__main__':
data = {
"database": "ClientsDB",
"collection": "clients",
}
mongo_obj = MongoAPI(data)
print(json.dumps(mongo_obj.read(), indent=4))