What if you could find any piece of data instantly, no matter how big your database grows?
Why Single field index in MongoDB? - Purpose & Use Cases
Imagine you have a huge phone book and you want to find all people named "John." Without any guide, you have to flip through every page one by one.
Searching manually through every entry is slow and tiring. It's easy to miss names or make mistakes, especially when the list grows very large.
A single field index acts like an organized list of all names, so you can quickly jump to all "Johns" without flipping every page.
db.contacts.find({name: 'John'}) // scans entire collectiondb.contacts.createIndex({name: 1});
db.contacts.find({name: 'John'}) // uses index for fast searchIt makes searching by one specific field lightning fast, even in huge collections.
When an online store wants to quickly find all orders by a customer's email, a single field index on the email field speeds up the search instantly.
Manual searching is slow and error-prone.
Single field index organizes data for quick lookup.
It dramatically speeds up queries on that field.
Practice
Solution
Step 1: Understand what single field index does
A single field index helps MongoDB find documents faster when searching or sorting by that one field.Step 2: Compare options with this purpose
Only To speed up queries that filter or sort by one specific field describes speeding up queries on one field, which matches the purpose of a single field index.Final Answer:
To speed up queries that filter or sort by one specific field -> Option BQuick Check:
Single field index = speed up queries on one field [OK]
- Thinking indexes store multiple fields
- Confusing indexes with backups
- Believing indexes create relationships
age in MongoDB?Solution
Step 1: Recall correct syntax for createIndex
The correct syntax uses an object with the field name as key and 1 for ascending order: createIndex({ field: 1 }).Step 2: Check each option
db.collection.createIndex({ age: 1 }) matches the correct syntax exactly. The other options use invalid syntax or wrong symbols.Final Answer:
db.collection.createIndex({ age: 1 }) -> Option CQuick Check:
Correct syntax = createIndex({ field: 1 }) [OK]
- Missing curly braces around the field
- Using wrong symbols like =>
- Using string 'asc' instead of 1
db.users.createIndex({ username: 1 })
db.users.find({ username: 'alice' })What is the effect of the index on the find query?
Solution
Step 1: Understand index effect on find queries
Creating an index on 'username' helps MongoDB quickly locate documents matching that field.Step 2: Analyze the find query
The find query filters by username 'alice', so it will use the index to speed up the search.Final Answer:
The query will use the index to find documents faster -> Option AQuick Check:
Index on field = faster find queries on that field [OK]
- Assuming indexes slow down queries
- Thinking queries ignore indexes
- Believing index causes errors
email using this command:db.users.createIndex(email: 1)
What is the problem with this command?
Solution
Step 1: Check syntax for createIndex
The argument to createIndex must be an object with curly braces: { field: 1 }.Step 2: Identify error in given command
The command lacks curly braces around email: 1, so it is invalid syntax.Final Answer:
Missing curly braces around the field and order -> Option DQuick Check:
createIndex needs { field: 1 } syntax [OK]
- Omitting curly braces
- Using strings instead of numbers for order
- Thinking any field cannot be indexed
score field in descending order. Which command correctly creates a single field index to optimize this?Solution
Step 1: Understand index order for sorting
To optimize descending sort, the index must be created with -1 for descending order.Step 2: Evaluate each option
db.users.createIndex({ score: -1 }) uses { score: -1 } which is correct. The option with { score: 1 } is ascending order, the option with { score: 'desc' } uses invalid string, the option with { score: 0 } uses 0 which is invalid.Final Answer:
db.users.createIndex({ score: -1 }) -> Option AQuick Check:
Descending index = { field: -1 } [OK]
- Using 1 for descending sort
- Using strings like 'desc' instead of numbers
- Using 0 which is invalid
Ready to prove it?
Pass the test and this pattern moves unknown
