A single field index helps the database find data faster when searching by one specific field.
Single field index in MongoDB
db.collection.createIndex({ fieldName: 1 })The 1 means the index is in ascending order.
You can also use -1 for descending order, but it usually works the same for single fields.
email field in the users collection.db.users.createIndex({ email: 1 })price field in the products collection.db.products.createIndex({ price: -1 })orderDate field in the orders collection.db.orders.createIndex({ orderDate: 1 })This example switches to the shopDB database, creates an ascending index on the category field in the products collection, then finds all products where the category is 'books'. The index helps this search run faster.
use shopDB // Create a single field index on the 'category' field db.products.createIndex({ category: 1 }) // Find all products in the 'books' category const results = db.products.find({ category: 'books' }).toArray() results
Creating an index takes some time and uses extra space, so only create indexes you really need.
Indexes speed up searches but slow down writes (inserts, updates) a bit because the index must be updated.
You can check existing indexes with db.collection.getIndexes().
Single field indexes speed up queries on one field.
Create them with createIndex({ field: 1 }).
Use them when you search or sort often by 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
