Featured resource
Forrester Wave Report 2025
Pluralsight named a Leader in the Forrester Wave™

Our tech skill development platform earned the highest scores possible across 11 criteria.

Learn more
  • Labs icon Lab
  • Core Tech
Labs

Guided: Working with a NoSQL Database in Python

This Code Lab introduces how to integrate a local MongoDB database into a Python application using the PyMongo driver. Throughout the lab, you will be responsible for establishing a connection to the MongoDB server as well as querying data using MongoDB’s native query syntax. The data you queried will then be used to complete the provided Python application so that it can be fully functional. The data you queried will then be used to complete the provided Python application so that it can be fully functional.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 30m
Published
Clock icon May 20, 2025

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Introduction

    NoSQL databases like MongoDB are commonly used to store and organize structured or semi-structured data in a flexible, schema-less format using collections and documents rather than rigid tables. In this lab, you will be working with MongoDB using PyMongo, the official MongoDB driver for Python. PyMongo allows developers to interact with MongoDB databases using Python functions and native data structures such as dictionaries and lists. While there are other tools and ORMs available for working with MongoDB, PyMongo is the most direct and widely adopted approach for interacting with MongoDB in Python.


    Application Scenario

    The premise of this lab is that you are trying to estimate ad revenue generated by your site whenever a user visits the site with a specific ad being displayed. If the user clicks the ad, the ad revenue generated gets multiplied by a specific amount. However, the percentage of users who actually click an ad typically varies depending on the advertiser. The formula that is used to estimate your ad revenue is a weighted sum that factors in the base rate paid per visitor, estimated click rate, and the multiplier per click.

    Your site has multiple advertisers that display content on it and each of them has their own values for base rates, click rates, and click multipliers that are stored in a MongoDB database. You will need to work with this database using PyMongo to query data and use it to calculate your ad revenue.


    Goals of the Application

    The first thing to note is that PyMongo is installed as a dependency in the Python virtual environment. To activate the virtual environment during this lab, use the command source .venv/bin/activate. You can check that the virtual environment is activated with which python, which should print a filepath pointing to a python exe within the .venv directory. You will also see workspace prefixed with .venv. To deactive this virtual environment, simply enter the command deactivate.

    The main goals you will need to accomplish with this lab are:

    1. Establish a connection with the MongoDB database.
    2. Interact with advertiser data using PyMongo.
    3. Use that data to estimate ad revenue with Python logic.

    To accomplish these goals, you will be working in the db/database.py, revenue.py, seed_data.py, and manage_ads.py files.

    IMPORTANT: As you complete each file, you will need to make sure you have a MongoDB server instance running with mongod --dbpath db/data in a separate terminal before you can execute the file in question.

    There is also a solution directory for you to check your implementations at any time. In addition, if you ever need to re-seed your database for whatever reason, use the command python seed_data.py once you have completed its implementation.

  2. Challenge

    Connecting to the Mongo Database

    Setting up the DB Connection

    First, you will need to setup the connection to the DB within db/database.py.

    To do this, you will need to utilize a MongoClient from the PyMongo driver. This client will take a connection string to connect to your database, which you can then use to retrieve a database from a running MongoDB server. From there, you can then access a collection of documents from that specific database to be used by the rest of the application.

    Instructions 1. Import `MongoClient` from `pymongo`. 2. Create a `MONGO_URI` variable set to the string `"mongodb://localhost:27017"`. This is the connection string that directs you to connect to a locally running MongoDB server. 3. Create a `client` variable using `MongoClient()`, passing in your `MONGO_URI` string. 4. Create a `db` variable that retrieves the `"ad_revenue_db"` from the `client`. 5. Create an `advertisers_collection` variable that retrieves `"advertisers"` from the `db` variable.

    After completing these steps, you will have set up your MongoClient to connect to your locally running MongoDB server instance. You will now able to interact with the database stored in this server instance in the next steps.

  3. Challenge

    Interacting with the DB

    Establishing a Utility Tool

    Now take a look at manage_ads.py. This utility file will be used as a way to interact with your database through commands such as add, delete, and list. The logic to read input arguments and execute these commands is covered within the main method of the file. Your main task will be to perform PyMongo API calls to ensure that the individual list_advertisers(), add_advertiser(), and delete_advertiser() functions perform as intended. Start with list_advertisers().

    Instructions 1. At the beginning of `list_advertisers()`, define a variable called `advertisers` and assign it to `list(advertisers_collection.find())`. This statement retrieves all the advertiser documents using `find()` and converts the result into a Python `List` for usage. 2. Check if `advertisers` is null. If it is, print the first statement and then `return`. 3. Otherwise, use a `for` loop to iterate through each `ad` in `advertisers()`. This should come after the first `"\n"` print statement. 4. The second print statement should be placed within this `for` loop. 5. The second `"\n"` statement should come after the `for` loop.

    Next, complete add_advertiser().

    Instructions 1. At the beginning of `add_advertiser()`, define a variable called `existing` and assign it to `advertisers_collection.find_one({"name": name})`. This will return a boolean on whether or not a document with the given name already exists in the database. 2. If `existing` is true, print the first statement and then `return`. 3. Otherwise, create a `new_advertiser` dict with key-value pairs that match the method parameters. 4. Create a `result` variable set to `advertisers_collection.insert_one(new_advertiser)`, which returns a result instance based on the object that was inserted. 5. In the second print statement, add an interpolation for `{result.inserted_id}` to specify the id of the newly added document.

    Lastly, complete delete_advertiser().

    Instructions 1. Wrap the print statements in a `try-except` block. 2. In the `try` block, define a `result` variable set to `advertisers_collection.delete_one({"_id": ObjectId(advertiser_id)})`. In this statement, the `advertiser_id` string is converted to an actual `ObjectId` type to be searched for. Once a document with a match is found, it is deleted. 3. If `result.deleted_count` is 0, then there was no matching document to be deleted, so print the first statement. Otherwise, print the second statement. 4. In the `except` block, catch a blanket `Exception` as `e`. Print the third statement here.

    Using the Tool

    Now that the utility file is finished, you have three commands at your disposal. The first is python manage_ads.py list, which will display the information (including ObjectId) of all advertisers currently in the database. The second is python manage_ads.py delete objIdHere, which deletes a document whose ObjectId matches the given id in place of objIdHere. Lastly, you can add a new advertiser with python manage_ads.py add name base_revenue click_rate click_rate_mult, replacing the last four arguments with your own user inputs. This will add a new advertiser to the database along with a uniquely generated ObjectId for it.


    Next Up

    You can now interact with your Mongo database, but there currently isn't any data in it outside of manually adding some on your own. The next step will look into how you can initially seed your database with some sample data, or reset it to a "starter" state. You will also be implementing the estimated advertiser revenue calculator using whatever advertiser data is in the database.

  4. Challenge

    Seeding Data and Calculating Revenue

    Populate the DB

    Now that you can interact with the advertiser data held in the DB, you need to set up your seed_data.py file to populate your database with some initial data. Head to seed_data.py and you will notice the initial import of your advertiser_collection from database.py. There is also a list containing sample advertiser data, followed by the function seed_advertisers(). You will need to perform some function calls from the PyMongo API to clean and seed the database with this list of sample advertisers.

    Instructions 1. At the start of the file, import your `advertisers_collection` from `db.database`. 2. In the `seed_advertisers()` function, make the call `advertisers_collection.delete_many({})` right before the print statement. The `delete_many` call ostensibly deletes multiple documents and passing in an empty object by default will remove all documents currently in the collection. 3. After calling `delete_many()`, make the call `insert_many(advertisers)` on `advertisers_collection`. This should also come before the print statement. In contrast to `delete_many()`, `insert_many()` inserts multiple documents into the collection. In this case, you are inserting all the advertisers in the `advertisers` list.

    With this finished you can now run python seed_data.py to populate (or reset) your database. You can also use the manage_ads utility tool to view these sample advertisers in your database, add more, or remove them using their ObjectIDs.


    Calculating Estimated Revenue

    Lastly, head over to revenue.py. This file contains the function that will calculate the estimated revenue generated from each advertiser per visitor using a weighted sum formula. However, you need to actually retrieve the data from your Mongo database for the function to use.

    Instructions 1. At the start of the file, import your `advertisers_collection` from `db.database`. 2. At the start of the `calculate_expected_revenue()` function, create a variable called `list_advertisers`. Assign this variable to `list(advertisers_collection.find())`.

    After completing this, you can execute python revenue.py display each advertiser and their expected revenue per visitor. You should see something like the following:

    Output
    Advertiser: PS-Alpha
    Expected Revenue per Visitor = $0.11
    
    Advertiser: PS-Tech
    Expected Revenue per Visitor = $0.09
    
    Advertiser: PS-Market
    Expected Revenue per Visitor = $0.12
    
    Advertiser: PS-Fit
    Expected Revenue per Visitor = $0.05
    
    Advertiser: PS-Industrial
    Expected Revenue per Visitor = $0.18
    
    Advertiser: PS-Stellar
    Expected Revenue per Visitor = $0.26
    

    Conclusion

    Congratulations on completing this lab! To reiterate what's been covered, you have done the following:

    1. Set up a connection with your Mongo database using the MongoClient provided by the pymongo library
    2. Implemented utility functions that allow you to interact with the database to add, remove, and display advertiser data
    3. Completed a seeding script that will initially populate or reset the advertiser data in your database to a predetermined state
    4. Utilized the advertiser data in your database to calculate estimated ad revenue as a weighted sum

George is a Pluralsight Author working on content for Hands-On Experiences. He is experienced in the Python, JavaScript, Java, and most recently Rust domains.

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.