-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRUD_Python_Module.py
More file actions
87 lines (79 loc) · 3.27 KB
/
Copy pathCRUD_Python_Module.py
File metadata and controls
87 lines (79 loc) · 3.27 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Example Python Code to Insert a Document
from pymongo import MongoClient
from bson.objectid import ObjectId
class AnimalShelter(object):
""" CRUD operations for Animal collection in MongoDB """
def __init__(self, username = "aacuser", password = "SNHU", host = "localhost", port = 27017, db = "AAC", col = "animals"):
# Initializing the MongoClient. This helps to access the MongoDB
# databases and collections. This is hard-wired to use the aac
# database, the animals collection, and the aac user.
#
# You must edit the password below for your environment.
#
# Connection Variables
#
USER = username
PASS = password
HOST = host
PORT = port
DB = db
COL = col
#HOST = 'localhost'
#PORT = 27017
#DB = 'aac'
#COL = 'animals'
#
# Initialize Connection
#
self.client = MongoClient('mongodb://%s:%s@%s:%d/?authSource=admin' % (USER,PASS,HOST,PORT))
self.database = self.client['%s' % (DB)]
self.collection = self.database['%s' % (COL)]
# Create a method to return the next available record number for use in the create method
# A method that inserts a document into a specified MongoDB database and collection
def create(self, data):
if isinstance(data, dict) and data:
try:
self.collection.insert_one(data) # data should be dictionary
return True
except Exception as e:
print(f"Something went wrong during insertion: {e}")
return False
else:
print("Invalid query type.")
return False
# A method that queries for documents from a specified MongoDB database and collection.
def read(self, query):
if isinstance(query, dict):
try:
cursor = self.collection.find(query)
return list(cursor)
except Exception as e:
print(f"Something went wrong: {e}")
return []
else:
print("Invalid query type.")
return []
# A method that queries documents and changes them from a specified MongoDB database and collection.
def update(self, query, data_update):
if isinstance(query, dict) and isinstance(data_update, dict) and query and data_update:
try:
result = self.collection.update_many(query, data_update)
return result.modified_count
except Exception as e:
print(f"Something went wrong during the update: {e}")
return 0
else:
print("Invalid query type.")
return 0
# A method that queries documents and deletes them from a specified MongoDB database and collection.
def delete(self, query):
if isinstance(query, dict) and query:
try:
result = self.collection.delete_many(query)
return result.deleted_count
except Exception as e:
print(f"Something went wrong during the deletion: {e}")
return 0
else:
print("Invalid query type.")
return 0