0
0
MongoDBquery~30 mins

Why managed databases matter in MongoDB - See It in Action

Choose your learning style9 modes available
Why Managed Databases Matter
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use createIndex with the field name and 1 for ascending order.