📖 Scenario: You are working for a small online store that wants to keep track of its products and orders. Instead of managing the database yourself, you decide to use a managed database service to make your work easier and safer.
🎯 Goal: Build a simple MongoDB database with collections for products and orders. Learn how to set up data, configure a query filter, retrieve specific data, and finalize the database structure.
📋 What You'll Learn
Create a products collection with exact product documents
Create an orders collection with exact order documents
Add a filter variable to select orders with quantity greater than 2
Write a query to find orders matching the filter
Add a final index on the orders collection for faster queries
💡 Why This Matters
🌍 Real World
Managed databases help small businesses easily store and retrieve data without worrying about server setup or maintenance.
💼 Career
Understanding how to create collections, query data, and optimize with indexes is essential for database administrators and backend developers.
Progress0 / 4 steps
1
Create the products and orders collections
Create a products collection with these exact documents: { _id: 1, name: "Pen", price: 1.5 }, { _id: 2, name: "Notebook", price: 3.0 }, and a collection called orders with these exact documents: { _id: 101, product_id: 1, quantity: 5 }, { _id: 102, product_id: 2, quantity: 1 }.
MongoDB
Hint
Use insertMany to add multiple documents to each collection.
2
Add a filter to select orders with quantity greater than 2
Create a variable called filter that holds the query { quantity: { $gt: 2 } } to find orders where quantity is greater than 2.
MongoDB
Hint
Use $gt operator to specify 'greater than' in MongoDB queries.
3
Query the orders collection using the filter
Write a query using db.orders.find(filter) to get all orders where quantity is greater than 2.
MongoDB
Hint
Use find method with the filter variable to get matching documents.
4
Add an index on the quantity field in orders
Create an index on the quantity field in the orders collection using db.orders.createIndex({ quantity: 1 }) to speed up queries.
MongoDB
Hint
Use createIndex with the field name and 1 for ascending order.
Practice
(1/5)
1. Why do managed databases matter for developers?
easy
A. They handle setup and maintenance so developers focus on the app
B. They require developers to manage backups manually
C. They limit the types of queries you can run
D. They only work with specific programming languages
Solution
Step 1: Understand the role of managed databases
Managed databases automate setup, maintenance, and scaling tasks.
Step 2: Identify the benefit for developers
This automation lets developers focus on building their app, not managing the database.
Final Answer:
They handle setup and maintenance so developers focus on the app -> Option A
Quick Check:
Managed DBs free developers from setup [OK]
Hint: Managed DBs automate setup and maintenance [OK]
Common Mistakes:
Thinking developers must manage backups manually
Believing managed DBs restrict query types
Assuming they only support certain languages
2. Which of the following is the correct way to connect to a managed MongoDB database using a connection string?
easy
A. sql://username:password@host:port/database
B. http://username:password@host:port/database
C. ftp://username:password@host:port/database
D. mongodb://username:password@host:port/database
Solution
Step 1: Recall MongoDB connection string format
MongoDB uses the mongodb:// protocol prefix for connections.
Step 2: Identify the correct option
Only mongodb://username:password@host:port/database uses the correct mongodb:// prefix with username, password, host, port, and database.
Final Answer:
mongodb://username:password@host:port/database -> Option D
Quick Check:
MongoDB connection strings start with mongodb:// [OK]
Hint: MongoDB URLs start with mongodb:// [OK]
Common Mistakes:
Using http:// or ftp:// instead of mongodb://
Omitting username or password in the string
Confusing port numbers or database names
3. Given a managed MongoDB collection users with documents: {name: 'Alice', age: 30}, {name: 'Bob', age: 25}, what will the query db.users.find({age: {$gt: 27}}) return?
medium
A. [{name: 'Alice', age: 30}]
B. [{name: 'Bob', age: 25}]
C. []
D. Syntax error
Solution
Step 1: Understand the query filter
The query looks for documents where age is greater than 27.
Step 2: Check which documents match
Alice has age 30 (greater than 27), Bob has 25 (not greater than 27).
Final Answer:
[{name: 'Alice', age: 30}] -> Option A
Quick Check:
age > 27 returns Alice only [OK]
Hint: Check which ages are greater than 27 [OK]
Common Mistakes:
Including Bob who is 25 (not > 27)
Expecting syntax error from valid query
Returning empty array incorrectly
4. You wrote this code to insert a document into a managed MongoDB collection:
db.users.insert({name: 'Carol', age: 28})
But it throws an error. What is the likely fix?
medium
A. Add a semicolon at the end
B. Use insertMany instead
C. Change insert to insertOne
D. Remove the age field
Solution
Step 1: Identify deprecated method usage
The insert method is deprecated in modern MongoDB drivers.
Step 2: Use the correct insertion method
Use insertOne to insert a single document without error.
Final Answer:
Change insert to insertOne -> Option C
Quick Check:
Use insertOne for single document inserts [OK]
Hint: Use insertOne for single document inserts [OK]
Common Mistakes:
Thinking semicolon fixes the error
Using insertMany for one document unnecessarily
Removing fields instead of fixing method
5. You want to ensure your managed MongoDB database automatically backs up data daily and scales during traffic spikes. Which feature of managed databases helps with this?
hard
A. Manual backup scheduling and fixed server size
B. Automated backups and auto-scaling
C. Only manual scaling with no backups
D. No backup or scaling features
Solution
Step 1: Identify backup and scaling needs
You want daily backups and automatic scaling during traffic spikes.
Step 2: Match features of managed databases
Managed databases provide automated backups and auto-scaling to meet these needs.
Final Answer:
Automated backups and auto-scaling -> Option B
Quick Check:
Managed DBs automate backups and scaling [OK]
Hint: Managed DBs automate backups and scaling [OK]
Common Mistakes:
Assuming backups must be manual
Thinking scaling is fixed and manual
Believing managed DBs lack these features
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.