-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoUtils.py
128 lines (108 loc) · 3.31 KB
/
mongoUtils.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
"""
Simple self contained class CRUD library for Pymongo
(client should not use import pymongo)
version 1.00
see MongoLibDemo for usage
You can copy this library in \python\mongolib
"""
import pymongo
import sys
class mutils:
def __init__(self):
#constructor
#private class variables
self.__errorMsg="No Error"
self.__client=""
self.__dbname=""
def setDB(self, db_name):
# Setting the current database
# Use this method when the login user has access to more than one database
self.__dbname=db_name
def getDB(self):
# get the currect database
return self.__dbname
def mConnect(self, connectionString):
# mConnent get a connection string in this format "user_name:user_pwrd@uri:port/defaultDB"
# return a list of available databases for this URL
# return False for any issues
# if you are going to work with multiple databases, you should set a database or create mConnect object for each database
# if you have access to multiple database, .
try:
self.__client = pymongo.MongoClient('mongodb://'+connectionString)
l=list()
try:
self.__dbname = self.__client.get_default_database().name
l.append(self.__client.get_default_database().name)
return l
except:
self.__errorMsg="Single Database"
try:
l = self.__client.database_names()
return l
except:
self.__errorMsg="Multiple Databases"
except:
self.__errorMsg=sys.exc_info()[0]
return [False]
def getDBColl(self):
# return list of collections per database
# error if the database does not exist
try:
dbo=self.__client[self.__dbname]
return list(dbo.collection_names())
except:
self.__errorMsg=sys.exc_info()[0]
return [False]
def getColDocs(self, co_name):
# parameter; collection name
# return list of documents per collection
try:
return list(self.__client[self.__dbname][co_name].find({}))
except:
self.__errorMsg=sys.exc_info()[0]
return [False]
def getErrMsg(self):
# get last error message
return self.__errorMsg
def delCollDocs(self, co_name, criteria ):
# parameters; collection name, criteria
# criteria is dictionary should be in this format {"id":""}
try:
db=self.__client[self.__dbname]
db[co_name].delete_many(criteria)
return [True]
except:
self.__errorMsg=sys.exc_info()[0]
return [False]
def findDoc(self, co_name, criteria):
# parameters; collection name, criteria
# criteria is a dictionary like {"id":""}
try:
print(criteria)
db=self.__client[self.__dbname]
return list(db[co_name].find(criteria))
except:
self.__errorMsg=sys.exc_info()[0]
return [False]
def updateDoc(self, co_name, filter, set_criteria, upsert_flag=False):
# parameters; collection name, filter, criteria, upsert flag (insert /update)
# filter, criteria is a dictionary like {"id":""}
# {"id":"MZZZZ"},{"$set": {"name":"qZZZZ"}}
# upsert default value is False
try:
db=self.__client[self.__dbname]
db[co_name].update_one(filter, set_criteria, upsert=upsert_flag)
return [True]
except:
self.__errorMsg=sys.exc_info()[0]
return [False]
def createDoc(self, co_name, doc):
# parameters; collection name, document
# document is a dictionary like {"id":""}
try:
db=self.__client[self.__dbname]
db[co_name].insert_one(doc)
return [True]
except:
self.__errorMsg=sys.exc_info()[0]
return [False]