0
0
MongoDBquery~30 mins

Why querying is essential in MongoDB - See It in Action

Choose your learning style9 modes available
Why Querying is Essential in MongoDB
📖 Scenario: You work at a small bookstore that keeps its inventory in a MongoDB database. The store manager wants to find specific books quickly, like all books by a certain author or books priced below a certain amount.
🎯 Goal: Build a simple MongoDB query to find books by a specific author and then filter books by price. This will show why querying is essential to get useful information from the database.
📋 What You'll Learn
Create a collection called books with sample book documents
Add a variable authorName to specify the author to search for
Write a query to find all books by authorName
Write a query to find all books priced less than a given amount
💡 Why This Matters
🌍 Real World
Bookstores, libraries, and many businesses use databases to store items. Querying helps find the right items quickly.
💼 Career
Database querying is a fundamental skill for data analysts, backend developers, and anyone working with data storage.
Progress0 / 4 steps
1
DATA SETUP: Create the books collection with sample data
Create a MongoDB collection called books and insert these exact documents: { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', price: 10 }, { title: '1984', author: 'George Orwell', price: 15 }, { title: 'To Kill a Mockingbird', author: 'Harper Lee', price: 12 }
MongoDB
Need a hint?

Use db.books.insertMany([...]) to add multiple documents at once.

2
CONFIGURATION: Define the authorName variable
Create a variable called authorName and set it to the string 'George Orwell' to specify the author you want to find.
MongoDB
Need a hint?

Use const authorName = 'George Orwell'; to create the variable.

3
CORE LOGIC: Query books by the author stored in authorName
Write a MongoDB query using db.books.find() to find all books where the author field matches the variable authorName.
MongoDB
Need a hint?

Use db.books.find({ author: authorName }) to get books by that author.

4
COMPLETION: Query books priced less than 13
Write a MongoDB query using db.books.find() to find all books where the price field is less than 13. Store the result in a variable called affordableBooks.
MongoDB
Need a hint?

Use { price: { $lt: 13 } } inside find() to filter by price less than 13.