Understanding MongoDB Indexes with a B-tree Mental Model
📖 Scenario: You are working on a small online bookstore database using MongoDB. You want to speed up searches for books by their title and author. To do this, you will create an index on these fields. This project will help you understand how MongoDB uses B-tree indexes to quickly find data.
🎯 Goal: Build a MongoDB collection with book data, create an index on the title and author fields, and query the collection using the index to find books efficiently.
📋 What You'll Learn
Create a MongoDB collection named books with specific book documents
Create a compound index on the title and author fields
Write a query that uses the index to find books by title and author
Understand the B-tree structure conceptually through the index creation
💡 Why This Matters
🌍 Real World
Indexes in MongoDB help online stores, libraries, and apps quickly find data like books, products, or users without scanning the entire database.
💼 Career
Understanding MongoDB indexes and their B-tree structure is essential for database administrators and developers to optimize query performance and build efficient applications.
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", year: 1925 }, { title: "1984", author: "George Orwell", year: 1949 }, and { title: "To Kill a Mockingbird", author: "Harper Lee", year: 1960 }.
MongoDB
Hint
Use db.books.insertMany() with an array of the three book objects.
2
Create a compound index on title and author
Create a compound index on the books collection for the fields title and author in ascending order using db.books.createIndex().
MongoDB
Hint
Use db.books.createIndex({ title: 1, author: 1 }) to create the index.
3
Query the books collection using the index
Write a query to find the book with title "1984" and author "George Orwell" using db.books.find().
MongoDB
Hint
Use db.books.find({ title: "1984", author: "George Orwell" }) to query.
4
Explain the B-tree index structure conceptually
Add a comment explaining that MongoDB uses a B-tree structure for the index created on title and author to quickly locate documents by searching through sorted keys.
MongoDB
Hint
Write a comment starting with // MongoDB uses a B-tree index structure explaining the index helps fast searching.
Practice
(1/5)
1. What is the main purpose of an index in MongoDB using a B-tree structure?
easy
A. To encrypt data for security
B. To store data in a compressed format
C. To speed up search and sort operations on data
D. To backup the database automatically
Solution
Step 1: Understand what a B-tree index does
A B-tree index organizes data so that searching and sorting can happen quickly.
Step 2: Identify the main benefit of indexing
Indexes help queries find data faster by avoiding full collection scans.
Final Answer:
To speed up search and sort operations on data -> Option C
Quick Check:
Index = Faster search and sort [OK]
Hint: Indexes speed up queries by organizing data [OK]
Common Mistakes:
Thinking indexes compress data
Confusing indexes with backups
Assuming indexes encrypt data
2. Which of the following is the correct syntax to create an ascending index on the field age in MongoDB?
easy
A. db.collection.createIndex({age: 1})
B. db.collection.createIndex(age: ascending)
C. db.collection.index({age: 'asc'})
D. db.collection.addIndex({age: 1})
Solution
Step 1: Recall MongoDB index creation syntax
The correct method is createIndex with a document specifying field and order.
Step 2: Check the correct format for ascending order
Ascending order is represented by 1, so {age: 1} is correct.
Final Answer:
db.collection.createIndex({age: 1}) -> Option A
Quick Check:
createIndex + {field: 1} = correct syntax [OK]
Hint: Use createIndex({field: 1}) for ascending index [OK]
Common Mistakes:
Using addIndex instead of createIndex
Passing string 'asc' instead of 1
Omitting curly braces around field
3. Given a collection with an index on score, what will be the order of documents returned by this query? db.collection.find().sort({score: 1})
medium
A. Documents sorted by score in descending order
B. Documents sorted by insertion order
C. Documents in random order
D. Documents sorted by score in ascending order
Solution
Step 1: Understand the sort parameter
The sort({score: 1}) means sort by score ascending (lowest to highest).
Step 2: Recognize index usage
Since there is an index on score, MongoDB uses it to return documents in ascending order efficiently.
Final Answer:
Documents sorted by score in ascending order -> Option D
Quick Check:
sort({score: 1}) = ascending order [OK]
Hint: sort({field: 1}) means ascending order [OK]
Common Mistakes:
Confusing 1 with descending order
Assuming no order without explicit index
Thinking insertion order is default
4. You created a unique index on the field email but still see duplicate emails in your collection. What is the likely cause?
medium
A. The index was created with ascending order instead of unique
B. The unique index was created after duplicates existed
C. MongoDB does not enforce uniqueness on indexes
D. The field email is missing in some documents
Solution
Step 1: Understand unique index behavior
Unique indexes prevent duplicates only for new inserts after index creation.
Step 2: Consider existing duplicates
If duplicates existed before creating the unique index, MongoDB allows them unless you drop or clean data first.
Final Answer:
The unique index was created after duplicates existed -> Option B
Quick Check:
Unique index blocks new duplicates, not old ones [OK]
Hint: Unique index blocks new duplicates, not existing ones [OK]
Common Mistakes:
Thinking ascending order affects uniqueness
Believing MongoDB ignores unique constraints
Assuming missing fields cause duplicates
5. You want to optimize queries filtering by category and sorting by price. Which index is best to improve performance?
hard
A. A compound index on {category: 1, price: 1}
B. Two separate indexes: one on category, one on price
C. A unique index on price
D. No index needed if collection is small
Solution
Step 1: Understand compound index benefits
A compound index on {category: 1, price: 1} supports queries filtering by category and sorting by price efficiently.
Step 2: Compare with separate indexes
Separate indexes cannot optimize combined filter and sort as well as a compound index.
Step 3: Consider uniqueness and collection size
Unique index on price is irrelevant here; small collections may not need indexes but that is not optimization.
Final Answer:
A compound index on {category: 1, price: 1} -> Option A
Quick Check:
Compound index = best for filter + sort [OK]
Hint: Use compound index for filter and sort fields together [OK]
Common Mistakes:
Using separate indexes instead of compound
Confusing unique index with performance index
Ignoring index for small collections
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.