Department of Information Systems ﻗﺴﻢ ﻧﻈﻢ اﻟﻤﻌﻠﻮﻣﺎت
LAB-4: Implement MongoDB with Python
Learning Objectives:
In this lab session you will learn:
1. To introduce how to work with MongoDB and PyMongo
2. To create a string connection from pyhton to local Mongodb Sever.
3. To create database, collections, and CRUD operation
4. Import JSON files into Python.
Prerequisites
1. Review theory lectures + previous lab sessions.
2. Any local Python IDE or editor
3. Python installed on your local machine.
4. MongoDB installed on your local machine and running on the default host and port.
5. A brief knowledge of Python language.
Learning Resources
1. W3Schools: Python MongoDB Tutorial
2. Python and MongoDB: Connecting to NoSQL Databases
3. JSON to MongoDB Python: Operations Simplified 101
Overview PyMongo Package
· In the previous lab sessions, you have learned the fundamentals of what MongoDB and how to
create and manage databases using the mongosh shell.
· In this lab sessions, we will use PyMongo library to access MongoDB Database in Python because
it contains the essential drivers required to connect Python to MongoDB
· In general, PyMongo provides a rich set of tools that you can use to communicate with a MongoDB
server. It provides functionality to query, retrieve results, write and delete data, and run database
commands.
· In this lab session, you will go through some examples that will help you get a feeling of how to
use PyMongo to create your own database applications with MongoDB and Python.
Exercise 4-1: Install and Use PyMongo Package
· To start using PyMongo, you first need to install it in your Python environment via
following steps:
1. Create a folder called ‘Lab04’ for example on your desktop.
2. In Anaconda IDE, open Jupter Notebook.
3. Create a new notebook and name it with ‘Lab4’ and save it inside the folder ‘Lab04’
4. Now, type this python command to install PyMongo package and then import it as follows:
!pip install pymongo
import pymongo
33 CIS Lab Manual – 2024 28
Department of Information Systems ﻗﺴﻢ ﻧﻈﻢ اﻟﻤﻌﻠﻮﻣﺎت
Exercise 4-2: Establishing a Connection with MongoClient
· To establish a connection to a database, you need to create a MongoClient instance. This class
provides a client for a MongoDB instance or server.
· Each client object has a built-in connection pool, which by default handles up to a hundred
connections to the server.
· MongoClient takes a set of arguments that allows you to specify custom host, port, and other
connection parameters.
· Steps:
1. In python notebook, import MongoClient from pymongo.
2. Then create a client object to communicate with your currently running MongoDB instance:
Note: The code above establishes a connection to the default host (localhost) and port (27017).
Exercise 4-3: Working with Databases in Python
· Once you have a connected instance of MongoClient, you can access any database managed by
the specified MongoDB server.
· To define which database you want to use, use the dot notation as shown below.
Note: If the database doesn’t exist, then MongoDB creates it for you, but only when you perform the
first operation on the database.
Exercise 4-4: Check the Existence of Database
· You can check if a database exist by listing all databases in you system Or you can check a
specific database by name.
33 CIS Lab Manual – 2024 29
Department of Information Systems ﻗﺴﻢ ﻧﻈﻢ اﻟﻤﻌﻠﻮﻣﺎت
Exercise 4-5: Check the Existence of Collections
Exercise 4-6: Working with Collections
Exercise 4-7: Creating Documents
· Storing data in your database using PyMongo is similar to what you did with the mongosh shell.
· But first, you need to create your documents using Python dictionaries:
· Steps:
33 CIS Lab Manual – 2024 30
Department of Information Systems ﻗﺴﻢ ﻧﻈﻢ اﻟﻤﻌﻠﻮﻣﺎت
Exercise 4-8: Querying Documents
· In lab 2, we have studied many ways of Querying documents which you can try here with
Python.
33 CIS Lab Manual – 2024 31
Department of Information Systems ﻗﺴﻢ ﻧﻈﻢ اﻟﻤﻌﻠﻮﻣﺎت
Exercise 4-9: Update One Document and Many Documents
Exercise 4-10: Delete One Document and Many Documents
Exercise 4-11: Close Connections
· Establishing a connection to a MongoDB database is typically an expensive operation.
· If you have an application that constantly retrieves and manipulates data in a MongoDB database,
then you probably don’t want to be opening and closing the connection all the time since this
might affect your application’s performance.
· In this kind of situation, you should keep your connection alive and only close it before exiting the
application to clear all the acquired resources.
· Command: client.close()
33 CIS Lab Manual – 2024 32
Department of Information Systems ﻗﺴﻢ ﻧﻈﻢ اﻟﻤﻌﻠﻮﻣﺎت
Exercise 4-12: Inserting Data from JSON to MongoDb Python
Task: to take the data from JSON files and insert the data into Mongodb database.
Steps:
1. Download the JSON files named data_one.json and data_many.json from this shared folder.
Save them in the lab04
2. Open the files and check their data.
3. Now implement the following codes in python:
Exercise 4-13: Self-Practice
· Refer to lab-2 and Lab-3 and practice their exercises using Python with MongoDB.
33 CIS Lab Manual – 2024 33