- Lab
- Data

Extract Customer Insights with MongoDB Queries
In this lab, you will explore how to extract customer insights using MongoDB queries. You will practice filtering, projecting, sorting, and aggregating data to identify high-value customers, segment user behavior, and analyze purchasing patterns. This hands-on lab builds your essential skills for data-driven decision-making using a NoSQL database.

Path Info
Table of Contents
-
Challenge
Introduction
Extract Customer Insights with MongoDB Queries
In this lab, you will explore how to extract customer insights using MongoDB queries. You will practice filtering, projecting, sorting, and aggregating data to identify high-value customers, segment user behavior, and analyze purchasing patterns.
You will write MongoDB code inside Python using the
pymongo
package. You do not need to know the Python syntax to complete this lab.
MongoDB methods:
In this lab, you will use the following MongoDB methods, along with many other operators:
Database
You will be working with the
customerdb
database, specifically thecustomers
collection. You will access this collection through a read-only user account.The collection contains seven fields, though some of them may be missing in certain documents, as outlined below:
first_name
: 12 documentslast_name
: 12 documentsemail
: 12 documentscountry
: 12 documentsactive
: 12 documentstotal_orders
: 10 documentslast_purchase
: 11 documents
Let's go! 🚀
-
Challenge
Step 1: Connect and filter customers
In the first step, you will connect to the database and perform filtering operations. You will work in the
src/step1.py
file to perform three tasks. Congratulations! You have completed the first of five steps in this lab. Be sure to runclient.close()
at the end of the script to close the MongoDB connection and free up resources.Let's move to the next step. 🚶➡️
-
Challenge
Step 2: Use logical operators
Not everyone belongs to a country, city, or a building. There are multiple parameters that you can associate with a user. Dealing with these parameters require the use of logical operators like
AND
,NOR
,GREATER THAN
, etc.In this step, you will learn how to work with logical operators in MongoDB. To implement them, you will work in the
src/step2.py
file.You must run the script using
python3 src/step2.py
in the Terminal. -
Challenge
Step 3: Project only necessary fields
Previously, you explored how to filter documents using logical operators and conditions. In this step, you will focus on limiting the fields in scope before applying any logical or conditional logic.
You will work in the
src/step3.py
file and you must use thepython3 src/step3.py
command in the Terminal to call the script functions. -
Challenge
Step 4: Sort documents
Sorting is essential in MongoDB queries when the order of results matters. For example, displaying the most recent purchases, top customers by order count, or alphabetical lists.
In this step, you will learn how to sort documents based on a single or multiple fields. You will work in the
src/step4.py
file and must use thepython3 src/step4.py
command to run the script in the Terminal. -
Challenge
Step 5: Limit the number of documents
Limiting the number of documents in a query helps reduce memory usage and improves performance, especially when dealing with large datasets.
In this final step, you will learn how to limit documents directly in a query and after applying operations like sorting or filtering.
For this task, you will work in the
src/step5.py
file and run the functions in the Terminal using the command:python3 src/step5.py
. Congratulation! 🥳🎉 You have completed all five lab steps.In this lab, you learned how to connect to a collection, filter documents using logical operators, project specific fields, sort results, and limit the number of documents returned.
You can now use these techniques to extract meaningful customer insights from your data.
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.