Bird
Raised Fist0
MongoDBquery~20 mins

Single field index in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Single Field Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Effect of Single Field Index on Query Performance

Consider a MongoDB collection users with a single field index on age. Which query will most likely use this index to improve performance?

MongoDB
db.users.createIndex({ age: 1 })
db.users.find({ age: { $gt: 30 } })
AThe query <code>db.users.find({ age: { $gt: 30 } })</code> uses the index and returns all users older than 30.
BThe query <code>db.users.find({ age: { $exists: true } })</code> uses the index to find all users with an age field.
CThe query <code>db.users.find({ name: 'Alice' })</code> uses the index on age to find users named Alice.
DThe query <code>db.users.find({ age: { $lt: 20 }, name: 'Bob' })</code> uses the index on age and name fields.
Attempts:
2 left
💡 Hint

Indexes help queries that filter on the indexed field.

🧠 Conceptual
intermediate
1:30remaining
Understanding Single Field Index Uniqueness

What is true about a single field index created with { field: 1 } in MongoDB?

AIt creates a non-unique index that speeds up queries filtering by that field.
BIt only indexes documents where the field exists.
CIt creates a unique index by default, preventing duplicate values in the field.
DIt automatically indexes all fields in the collection.
Attempts:
2 left
💡 Hint

Think about default behavior of indexes in MongoDB.

📝 Syntax
advanced
1:30remaining
Correct Syntax to Create a Single Field Index

Which of the following commands correctly creates a single field ascending index on the email field in MongoDB?

Adb.collection.createIndex(['email', 1])
Bdb.collection.createIndex(email: 1)
Cdb.collection.createIndex({ 'email' => 1 })
Ddb.collection.createIndex({ email: 1 })
Attempts:
2 left
💡 Hint

Remember the syntax for specifying fields in an object.

❓ optimization
advanced
2:00remaining
Choosing the Best Index for Query Optimization

You have a collection with fields category and price. You often run queries filtering only by category. Which index is best to optimize these queries?

AA compound index on <code>category</code> and <code>price</code>.
BA single field index on <code>category</code> only.
CA single field index on <code>price</code> only.
DNo index is needed for filtering by <code>category</code>.
Attempts:
2 left
💡 Hint

Indexes should match the fields used in query filters.

🔧 Debug
expert
2:30remaining
Diagnosing Query Performance Issue with Single Field Index

A MongoDB collection has a single field index on status. A query db.orders.find({ status: { $in: ['shipped', 'delivered'] } }) is slow. What is the most likely reason?

AThe <code>$in</code> operator cannot use single field indexes.
BThe index on <code>status</code> is not being used because the query uses an array.
CThe index exists but the query is slow because of missing index statistics or outdated index.
DThe collection is too large and needs a compound index including <code>status</code> and <code>date</code>.
Attempts:
2 left
💡 Hint

Think about index usage and maintenance in MongoDB.

Practice

(1/5)
1. What is the main purpose of creating a single field index in MongoDB?
easy
A. To backup the database automatically
B. To speed up queries that filter or sort by one specific field
C. To store multiple fields in a single document
D. To create relationships between collections

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    To speed up queries that filter or sort by one specific field -> Option B
  4. Quick Check:

    Single field index = speed up queries on one field [OK]
Hint: Indexes speed up queries on the indexed field [OK]
Common Mistakes:
  • Thinking indexes store multiple fields
  • Confusing indexes with backups
  • Believing indexes create relationships
2. Which of the following is the correct syntax to create a single field ascending index on the field age in MongoDB?
easy
A. db.collection.createIndex({ 'age' => 1 })
B. db.collection.createIndex(age: 1)
C. db.collection.createIndex({ age: 1 })
D. db.collection.createIndex({ age: 'asc' })

Solution

  1. 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 }).
  2. Step 2: Check each option

    db.collection.createIndex({ age: 1 }) matches the correct syntax exactly. The other options use invalid syntax or wrong symbols.
  3. Final Answer:

    db.collection.createIndex({ age: 1 }) -> Option C
  4. Quick Check:

    Correct syntax = createIndex({ field: 1 }) [OK]
Hint: Use curly braces and 1 for ascending index [OK]
Common Mistakes:
  • Missing curly braces around the field
  • Using wrong symbols like =>
  • Using string 'asc' instead of 1
3. Given the following commands run on a MongoDB collection:
db.users.createIndex({ username: 1 })
db.users.find({ username: 'alice' })

What is the effect of the index on the find query?
medium
A. The query will use the index to find documents faster
B. The query will cause an error due to index usage
C. The query will ignore the index and scan all documents
D. The query will run slower because of the index

Solution

  1. Step 1: Understand index effect on find queries

    Creating an index on 'username' helps MongoDB quickly locate documents matching that field.
  2. Step 2: Analyze the find query

    The find query filters by username 'alice', so it will use the index to speed up the search.
  3. Final Answer:

    The query will use the index to find documents faster -> Option A
  4. Quick Check:

    Index on field = faster find queries on that field [OK]
Hint: Find queries on indexed field run faster [OK]
Common Mistakes:
  • Assuming indexes slow down queries
  • Thinking queries ignore indexes
  • Believing index causes errors
4. You tried to create a single field index on email using this command:
db.users.createIndex(email: 1)

What is the problem with this command?
medium
A. Indexes cannot be created on the email field
B. Using 1 instead of 'asc' for ascending order
C. The field name should be a string in quotes
D. Missing curly braces around the field and order

Solution

  1. Step 1: Check syntax for createIndex

    The argument to createIndex must be an object with curly braces: { field: 1 }.
  2. Step 2: Identify error in given command

    The command lacks curly braces around email: 1, so it is invalid syntax.
  3. Final Answer:

    Missing curly braces around the field and order -> Option D
  4. Quick Check:

    createIndex needs { field: 1 } syntax [OK]
Hint: Always use curly braces for index fields [OK]
Common Mistakes:
  • Omitting curly braces
  • Using strings instead of numbers for order
  • Thinking any field cannot be indexed
5. You want to speed up queries that sort users by their score field in descending order. Which command correctly creates a single field index to optimize this?
hard
A. db.users.createIndex({ score: -1 })
B. db.users.createIndex({ score: 1 })
C. db.users.createIndex({ score: 'desc' })
D. db.users.createIndex({ score: 0 })

Solution

  1. Step 1: Understand index order for sorting

    To optimize descending sort, the index must be created with -1 for descending order.
  2. 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.
  3. Final Answer:

    db.users.createIndex({ score: -1 }) -> Option A
  4. Quick Check:

    Descending index = { field: -1 } [OK]
Hint: Use -1 for descending index order [OK]
Common Mistakes:
  • Using 1 for descending sort
  • Using strings like 'desc' instead of numbers
  • Using 0 which is invalid
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard