0
0
MongoDBquery~30 mins

Why the paradigm shift matters in MongoDB - See It in Action

Choose your learning style9 modes available
Why the paradigm shift matters
📖 Scenario: You are helping a small bookstore switch from a traditional paper catalog to a modern database system using MongoDB. This will help the store manage books and sales more easily and quickly.
🎯 Goal: Build a simple MongoDB collection to store book information, add a configuration for filtering books by price, query the collection to find affordable books, and finalize the setup for easy future updates.
📋 What You'll Learn
Create a MongoDB collection named books with exactly three book documents
Add a variable maxPrice to set the maximum price for affordable books
Write a query to find all books with a price less than or equal to maxPrice
Add an index on the price field to optimize queries
💡 Why This Matters
🌍 Real World
Bookstores and many businesses use databases like MongoDB to store and quickly find information about products, customers, and sales.
💼 Career
Knowing how to create collections, query data, and optimize with indexes is essential for database administrators and developers working with NoSQL databases.
Progress0 / 4 steps
1
DATA SETUP: Create the books collection with three books
Create a MongoDB collection called books and insert exactly these three documents: { title: "The Great Gatsby", author: "F. Scott Fitzgerald", price: 10 }, { title: "1984", author: "George Orwell", price: 15 }, and { title: "To Kill a Mockingbird", author: "Harper Lee", price: 12 }.
MongoDB
Need a hint?

Use db.books.insertMany([...]) with the exact book documents inside the array.

2
CONFIGURATION: Define maxPrice to filter affordable books
Create a variable called maxPrice and set it to 12 to represent the maximum price for affordable books.
MongoDB
Need a hint?

Use const maxPrice = 12 to create the variable.

3
CORE LOGIC: Query books with price less than or equal to maxPrice
Write a MongoDB query using db.books.find() to find all books where the price is less than or equal to the variable maxPrice.
MongoDB
Need a hint?

Use db.books.find({ price: { $lte: maxPrice } }) to get books priced at or below maxPrice.

4
COMPLETION: Add an index on the price field
Create an index on the price field in the books collection using db.books.createIndex() to speed up price queries.
MongoDB
Need a hint?

Use db.books.createIndex({ price: 1 }) to create an ascending index on the price field.