forked from SebastienBtr/capitrain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
23 lines (20 loc) · 758 Bytes
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import os
import pymongo
MONGO_CLUSTER_ADDRESS = os.getenv('MONGO_CLUSTER_ADDRESS')
MONGO_DB_NAME = os.getenv('MONGO_DB_NAME')
MONGO_DB_USER = os.getenv('MONGO_DB_USER')
MONGO_DB_PASSWORD = os.getenv('MONGO_DB_PASSWORD')
db = None
# Connect to db
def connect_to_db():
global db
cluster = pymongo.MongoClient(
"mongodb+srv://{}:{}@{}/test?retryWrites=true&w=majority".format(MONGO_DB_USER, MONGO_DB_PASSWORD, MONGO_CLUSTER_ADDRESS))
db = cluster[MONGO_DB_NAME]
# Insert a document in the given collection
def save_element(element, collection_name):
global db
if db is None:
raise Exception("Please connect to database before inserting an element")
collection = db[collection_name]
collection.insert_one(element)