-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect.py
More file actions
55 lines (37 loc) · 1.2 KB
/
Copy pathconnect.py
File metadata and controls
55 lines (37 loc) · 1.2 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
from pymongo import MongoClient #need to install pymongo first [pip install pymongo]
try:
client = MongoClient("mongodb://127.0.0.1:27017")
print("Connected successfully!!!")
except:
print("Could not connect to MongoDB")
# Access database
mydatabase = client['cscorner'] # If not already created, a new database will be created
# Access collection of the database
mycollection=mydatabase['students']
item_1 = {
"_id" : "U1IT00001",
"Name" : "Riz",
"ID" : 2,
}
mycollection.insert_one(item_1)
cursor = mycollection.find()
for record in cursor: # prints all the records
print(record)
client.close()
#The following one returns the database
# from pymongo import MongoClient
# def get_database():
# client = MongoClient("mongodb://127.0.0.1:27017")
# mydatabase = client['cs']
# mycollection=mydatabase['students2']
# item_2 = {
# "Name" : "Riz2",
# "ID" : 22,
# }
# mycollection.insert_one(item_2)
# return client['cs']
# # This is added so that many files can reuse the function get_database()
# if __name__ == "__main__":
# # Get the database
# dbname = get_database()
# print(dbname)