📖 Scenario: You are managing a small online bookstore database using MongoDB. You want to understand how different query patterns affect the speed and efficiency of finding books.
🎯 Goal: Build a simple MongoDB collection with book data, add an index, and write queries to see how query patterns impact performance.
📋 What You'll Learn
Create a MongoDB collection called books with specific book documents
Add an index on the author field
Write a query to find books by a specific author
Write a query to find books by a specific author and genre
💡 Why This Matters
🌍 Real World
Online bookstores and many apps use databases like MongoDB to store and find data quickly. Understanding query patterns helps keep the app fast.
💼 Career
Database developers and backend engineers must write efficient queries and create indexes to optimize data retrieval in real projects.
Progress0 / 4 steps
1
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", genre: "Classic" }, { title: "1984", author: "George Orwell", genre: "Dystopian" }, { title: "To Kill a Mockingbird", author: "Harper Lee", genre: "Classic" }
MongoDB
Hint
Use db.books.insertMany() with an array of book objects.
2
Add an index on the author field
Create an index on the author field in the books collection using db.books.createIndex.
MongoDB
Hint
Use db.books.createIndex({ author: 1 }) to create an ascending index on the author field.
3
Write a query to find books by author
Write a query using db.books.find to find all books where the author is exactly "George Orwell".
MongoDB
Hint
Use db.books.find({ author: "George Orwell" }) to get books by that author.
4
Write a query to find books by author and genre
Write a query using db.books.find to find all books where the author is "Harper Lee" and the genre is "Classic".
MongoDB
Hint
Use db.books.find({ author: "Harper Lee", genre: "Classic" }) to filter by both fields.
Practice
(1/5)
1. Why is it important to use good query patterns in MongoDB?
easy
A. They help find data faster and use less resources.
B. They make the database larger in size.
C. They slow down the application.
D. They remove data automatically.
Solution
Step 1: Understand query patterns
Good query patterns are designed to find data quickly and efficiently.
Step 2: Recognize benefits
Using good patterns reduces resource use and speeds up the app.
Final Answer:
They help find data faster and use less resources. -> Option A
Quick Check:
Good query patterns = faster data access [OK]
Hint: Good queries = faster results and less resource use [OK]
Common Mistakes:
Thinking query patterns increase database size
Believing query patterns slow down the app
Assuming query patterns delete data
2. Which of the following is the correct way to write a simple MongoDB query to find documents where age is 25?
easy
A. db.collection.find({age: "25"})
B. db.collection.find(age == 25)
C. db.collection.find({age: 25})
D. db.collection.find({age: $eq: 25})
Solution
Step 1: Check MongoDB query syntax
The correct syntax uses a JSON object with field and value inside find().
Step 2: Identify correct option
db.collection.find({age: 25}) uses {age: 25} which is valid syntax for equality.
Hint: Use JSON object with field: value inside find() [OK]
Common Mistakes:
Using == inside find() instead of JSON object
Putting value as string when it should be number
Incorrect use of $eq without braces
3. Given the collection users with documents: {name: "Alice", age: 30}, {name: "Bob", age: 25}, {name: "Carol", age: 30} What will db.users.find({age: 30}) return?
medium
A. [{name: "Bob", age: 25}]
B. [{name: "Alice", age: 30}, {name: "Carol", age: 30}]
C. []
D. SyntaxError
Solution
Step 1: Understand the query condition
The query looks for documents where age equals 30.
Step 2: Match documents in collection
Alice and Carol have age 30, so both match and are returned.