0% found this document useful (0 votes)
74 views5 pages

Practical 4.2 MongoDB

Uploaded by

black hello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views5 pages

Practical 4.2 MongoDB

Uploaded by

black hello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

BMCS 2013 DATA ENGINEERING 1 of

PRACTICAL 4.2 MongoDB

MongoDB Atlas is a cloud database that runs on the cloud providers such as AWS, GCP,
and Azure. It enables users to host Database Clusters and connect to them remotely.

💡 The following instructions were correct at the time of writing. If there have been any
updates to the MongoDB Atlas website since then, do follow the new instructions
provided there.

1. MongoDB Atlas Sign Up


Sign-up for a free MongoDB Atlas account. Follow the instructions given in this
video to create a free cluster.
BMCS 2013 DATA ENGINEERING 2 of
5
2. Connecting to the MongoDB Atlas Cluster

2.1. Click the Connect button:

2.2. Under Connect to your application, click Drivers:

2.3. Connecting to your cluster


BMCS 2013 DATA ENGINEERING 3 of
5
Follow the instructions shown in the screenshot below***:

***Important: take note of the following details:


2.3.1. In step 1, for Driver, select “Python” and for Version, select “3.11
or later”

2.3.2. In step 2, install the driver in your venv.

2.3.3. In step 3, replace the password in the connection string as


instructed. This connection string will be used for connecting to
your cluster.
BMCS 2013 DATA ENGINEERING 4 of
5
3. Writing Code using PyMongo
Reference MongoDB University: Python Developer Path

3.1. Install the library in your venv


$ pip install python-dateutil

3.2. Create a centralised file for your MongoDB connection code


Create a Python file named pymongo_utils.py with the following lines of code.

from pymongo import MongoClient

# Replace with your connection string and password


CONNECTION_STRING = "<your connection string>"

class PyMongoUtils:

def __init__(self, uri=CONNECTION_STRING):


self.uri = uri

def get_database(self, database_name):


client = MongoClient(self.uri)
return client[database_name]

3.3. Write code to insert documents into your collection

3.4. In MongoDB Atlas, use Browse Collections to see the newly


inserted document
BMCS 2013 DATA ENGINEERING 5 of
5

3.5. Write code to query data

3.6. Write code to convert the list of dictionaries to a PySpark


DataFrame

You might also like