Wk 2 5 Accessing MongoDB Using Python
Wk 2 5 Accessing MongoDB Using Python
Objectives
After completing this lab you will be able to:
Access the MongoDB database from Python with the pymongo driver
Perform basic operations such as selecting, inserting and listing using Python
Create a Python program to run the MongoDB operations
Prerequisites
Before starting this lab, it’ll be helpful to have knowledge about basic Python and MongoDB operations. If you’re unfamiliar with MongoDB, feel free to take a
look at the Getting Started with MongoDB and MongoDB CRUD labs!
Copied!
1. start_mongo
Copied!
Note down the user name and password, that are be displayed in the last line of the output of the start_mongo command.
about:blank 1/6
10/4/23, 10:20 AM about:blank
Give the name as mongo_connect.py, as in the figure below, and click on OK.
Copy and paste the below code into the newly opened file.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
12. 12
13. 13
14. 14
15. 15
16. 16
17. 17
18. 18
19. 19
20. 20
21. 21
22. 22
23. 23
about:blank 2/6
10/4/23, 10:20 AM about:blank
9. print("Connecting to mongodb server")
10. connection = MongoClient(connecturl)
11.
12. # get database list
13. print("Getting list of databases")
14. dbs = connection.list_database_names()
15.
16. # print the database names
17.
18. for db in dbs:
19. print(db)
20. print("Closing the connection to the mongodb server")
21.
22. # close the server connecton
23. connection.close()
Copied!
PLEASE ENSURE THAT YOU HAVE REPLACED THE PASSWORD VALUE IN THE FILE ABOVE WITH THE PASSWORD FOR YOUR MONGODB
SERVER THAT YOU COPIED AFTER IT WAS STARTED.
Save the code file using the File->Save menu option as in the image below.
Copy and paste the below code on the terminal to execute this file.
1. 1
1. python3 mongo_connect.py
Copied!
You should see an output like the one in the image below.
about:blank 3/6
10/4/23, 10:20 AM about:blank
Copy and paste the below code into mongo_query.py.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
12. 12
13. 13
14. 14
15. 15
16. 16
17. 17
18. 18
19. 19
20. 20
21. 21
22. 22
23. 23
24. 24
25. 25
26. 26
27. 27
28. 28
29. 29
30. 30
31. 31
32. 32
33. 33
34. 34
35. 35
36. 36
37. 37
38. 38
39. 39
40. 40
Copied!
PLEASE ENSURE THAT YOU HAVE REPLACED THE PASSWORD VALUE IN THE FILE ABOVE WITH THE PASSWORD FOR YOUR MONGODB
SERVER THAT YOU COPIED AFTER IT WAS STARTED.
about:blank 4/6
10/4/23, 10:20 AM about:blank
1. 1
1. python3 mongo_query.py
Copied!
Practice exercise
Write a Python program that can:
{“document”:”a document contains the data in the form of key value pairs.”}
query and print all the documents in the training database and mongodb_glossary collection.
close the connection to the server.
Copied!
Authors
Ramesh Sannareddy
Other Contributors
Rav Ahuja
Change Log
Date (YYYY-MM-DD) Version Changed By Change Description
2022-12-22 0.5 K Sundararajan Updated pip command for pymongo installation
2021-11-17 0.4 Kathy An Updated lab instructions
2021-04-19 0.3 Steve Ryan Review pass
2021-03-19 0.2 Ramesh Sannareddy Added practice exercises.
2021-02-24 0.1 Ramesh Sannareddy Created initial version of the lab
about:blank 6/6