Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why querying nested data matters
📖 Scenario: You work at a company that stores customer orders in a MongoDB database. Each order document contains nested information about the customer and the items they purchased. You want to learn how to query this nested data to find specific details.
🎯 Goal: Build a MongoDB query step-by-step to find orders where a specific item was purchased and extract nested customer information.
📋 What You'll Learn
Create a collection called orders with sample nested documents
Add a filter variable to specify the item name to search for
Write a query to find orders containing that item in the nested items array
Project the customer's name and the matching item details in the query result
💡 Why This Matters
🌍 Real World
Many real-world databases store complex nested data like orders with customers and items. Being able to query inside nested arrays is essential to find relevant information quickly.
💼 Career
Database developers and analysts often need to write queries that access nested data structures to generate reports, filter data, or build APIs.
Progress0 / 4 steps
1
DATA SETUP: Create the orders collection with nested documents
Create a variable called orders and assign it an array with these two documents exactly: { _id: 1, customer: { name: "Alice", age: 30 }, items: [ { name: "Pen", qty: 3 }, { name: "Notebook", qty: 1 } ] } and { _id: 2, customer: { name: "Bob", age: 25 }, items: [ { name: "Pencil", qty: 2 }, { name: "Pen", qty: 5 } ] }
MongoDB
Hint
Define orders as a list with two dictionaries. Each dictionary has _id, customer (which is another dictionary), and items (which is a list of dictionaries).
2
CONFIGURATION: Define the item name to search for
Create a variable called search_item and set it to the string "Pen".
MongoDB
Hint
Just create a variable search_item and assign it the string "Pen".
3
CORE LOGIC: Write a MongoDB-style query to find orders with the search item
Create a variable called matching_orders and assign it a list comprehension that filters orders for documents where any item in items has name equal to search_item. Use for order in orders and any(item['name'] == search_item for item in order['items']) in the condition.
MongoDB
Hint
Use a list comprehension with any() to check if any item in order['items'] has the name matching search_item.
4
COMPLETION: Project customer name and matching item details
Create a variable called result and assign it a list comprehension that for each order in matching_orders creates a dictionary with keys customer_name set to order['customer']['name'] and items set to a list of items from order['items'] where item['name'] == search_item.
MongoDB
Hint
Use a list comprehension over matching_orders. For each order, create a dictionary with customer_name and a filtered list of items matching search_item.
Practice
(1/5)
1. What is the main reason to use dot notation when querying nested data in MongoDB?
easy
A. To access fields inside embedded documents
B. To update the entire document at once
C. To delete the whole collection
D. To create a new database
Solution
Step 1: Understand nested data structure
Nested data means one document contains another document inside it.
Step 2: Use dot notation to access inner fields
Dot notation lets you specify the path to a field inside the embedded document.
Final Answer:
To access fields inside embedded documents -> Option A
Quick Check:
Dot notation = access nested fields [OK]
Hint: Dot notation drills down into nested fields fast [OK]
Common Mistakes:
Thinking dot notation updates whole documents
Confusing dot notation with collection operations
Using dot notation to create databases
2. Which of the following is the correct MongoDB query syntax to find documents where the nested field address.city equals "Paris"?
easy
A. { "address.city": "Paris" }
B. { address: { city: "Paris" } }
C. { address.city = "Paris" }
D. { address->city: "Paris" }
Solution
Step 1: Recall MongoDB query syntax for nested fields
MongoDB uses dot notation inside quotes to query nested fields.
Step 2: Identify correct syntax
{ "address.city": "Paris" } uses "address.city" as a string key with value "Paris", which is correct.
Final Answer:
{ "address.city": "Paris" } -> Option A
Quick Check:
Dot notation in quotes = correct query [OK]
Hint: Use quotes and dot notation for nested field queries [OK]
Common Mistakes:
Using object syntax without quotes for nested fields