forked from ebubekirtabak/PySloth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.py
65 lines (50 loc) · 1.71 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
from pymongo import MongoClient
import logger
import sys
# http://api.mongodb.com/python/current/tutorial.html
from pprint import pprint
def connect_database(database):
print(database)
client = MongoClient(database['uri'])
try:
if database['name'] in client.list_database_names():
db = client[database['name']]
return db
else:
print('Database not exixts, create database: ' + database['name'])
db = client[database['name']]
return db
except Exception as e:
print("Error:" + str(e))
return 400
def get_server_status(db):
try:
return db.command("serverStatus")
except Exception as e:
print(e)
return 0
def insert(db, collection, data):
if db != 400:
try:
mycol = db[collection]
result = mycol.insert_one(data)
if result.inserted_id is not None:
logger.Logger().set_log("insert data")
else:
logger.Logger().set_error_log("mongo insert data error")
except Exception as e:
logger.Logger().set_error_log("mongoDB inser Error: " + str(e))
# type, value, traceback = sys.exc_info()
# print('Error opening %s: %s' % (value.filename, value.strerror))
print(e)
return 400
# def get_object(db, collection):
def get_find_one(db, collection, query):
return db[collection].find(query)
def find_and_delete(db, collection, query):
return db[collection].find_one_and_delete(query)
def get_length(db, collection):
return db[collection].count()
def isExists(db, collection, data):
mycol = db[collection]
return mycol.find(data).count() > 0