-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnection.py
More file actions
37 lines (30 loc) · 1.29 KB
/
connection.py
File metadata and controls
37 lines (30 loc) · 1.29 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
# PyMongo is a Python distribution containing tools for working with MongoDB
from pymongo import MongoClient
# creating database if doesnt exist
def getDatabase():
cluster = MongoClient(
"mongodb+srv://root:root@cluster0.o8ehzeq.mongodb.net/?retryWrites=true&w=majority")
db = cluster["my_database"]
return db
# inserting single value on collection
def insert_user(data, collection):
collection.insert_one(data)
print("Congrats User Succesfully inserted " , data)
# deleting single value on collection
def delete_user(query, collection):
collection.delete_one(query)
print("Congrats User Succesfully deleted " , query )
# updating single value n the collection
def update_user(query, collection, data_to_update):
collection.update_one(query, data_to_update)
print("Congrats User Succesfully updated " , query , "To" , data_to_update )
# main function
def main():
database = getDatabase()
collection = database["user_collection"]
# insert_user(data= {"_id": 100, "user_name": "Sushil"} , collection=collection)
# delete_user(query = {"_id": 100} , collection=collection)
# update_user(query={"_id": 100}, collection=collection , data_to_update= {"$set" : {"user_name" : "Gopal Meena"}} )
# calling main function
if __name__ == "__main__":
main()