Bird
Raised Fist0
MongoDBquery~20 mins

createIndex method in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
MongoDB Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What does this createIndex command do?
Consider the following MongoDB command:
db.users.createIndex({ email: 1 }, { unique: true })

What is the effect of this command on the users collection?
MongoDB
db.users.createIndex({ email: 1 }, { unique: true })
ACreates a descending index on the email field but allows duplicate email values.
BRemoves all documents with duplicate email values from the collection.
CCreates a text index on the email field for full-text search.
DCreates an ascending index on the email field and enforces unique email values in the collection.
Attempts:
2 left
💡 Hint
Think about what the 1 and unique: true mean in the index creation.
❓ query_result
intermediate
2:00remaining
What is the output of this createIndex command?
Given the command:
db.orders.createIndex({ orderDate: -1 })

What kind of index is created on the orderDate field?
MongoDB
db.orders.createIndex({ orderDate: -1 })
AA descending index on orderDate.
BA hashed index on orderDate.
CAn ascending index on orderDate.
DA compound index on orderDate and another field.
Attempts:
2 left
💡 Hint
The value -1 indicates the index order direction.
📝 Syntax
advanced
2:00remaining
Which createIndex command is syntactically correct?
Choose the correct MongoDB createIndex command syntax to create a compound index on fields firstName (ascending) and lastName (descending):
Adb.collection.createIndex({ firstName: 'asc', lastName: 'desc' })
Bdb.collection.createIndex([ firstName: 1, lastName: -1 ])
Cdb.collection.createIndex({ firstName: 1, lastName: -1 })
Ddb.collection.createIndex({ firstName => 1, lastName => -1 })
Attempts:
2 left
💡 Hint
Remember the syntax for specifying fields and order in createIndex.
❓ optimization
advanced
2:00remaining
Which createIndex option improves query performance for frequent searches on a field with many duplicate values?
You have a collection with a field status that has only a few distinct values repeated many times. Which index type should you create to optimize queries filtering by status?
ACreate a unique index on the status field.
BCreate a hashed index on the status field.
CCreate a sparse index on the status field.
DCreate a multikey index on the status field.
Attempts:
2 left
💡 Hint
Think about how hashed indexes distribute values for equality queries.
🔧 Debug
expert
3:00remaining
Why does this createIndex command fail?
You run this command:
db.products.createIndex({ price: 1 }, { unique: true })

But it fails with an error about duplicate keys. Why?
MongoDB
db.products.createIndex({ price: 1 }, { unique: true })
ABecause there are multiple documents with the same price value violating the unique constraint.
BBecause the createIndex method requires a second field for uniqueness.
CBecause unique indexes cannot be created on numeric fields.
DBecause the price field cannot be indexed in ascending order.
Attempts:
2 left
💡 Hint
Unique indexes require all indexed values to be different.

Practice

(1/5)
1. What is the main purpose of the createIndex method in MongoDB?
easy
A. To speed up queries by creating an index on a field
B. To delete documents from a collection
C. To update multiple documents at once
D. To create a new collection

Solution

  1. Step 1: Understand what createIndex does

    The createIndex method organizes data to make searches faster.
  2. Step 2: Identify the correct purpose

    It does not delete or update documents, nor create collections, only speeds up queries.
  3. Final Answer:

    To speed up queries by creating an index on a field -> Option A
  4. Quick Check:

    createIndex speeds up queries [OK]
Hint: Remember: createIndex speeds up searches, not data changes [OK]
Common Mistakes:
  • Confusing createIndex with delete or update operations
  • Thinking createIndex creates collections
  • Assuming createIndex modifies documents
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: 1)
C. db.collection.createIndex({ age: -1 })
D. db.collection.createIndex({ age })

Solution

  1. Step 1: Recall syntax for createIndex

    The method requires an object with field names as keys and 1 or -1 as values for ascending or descending.
  2. Step 2: Identify correct ascending index syntax

    { age: 1 } means ascending index on age. Other options are either descending (-1) or invalid syntax.
  3. Final Answer:

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

    Ascending index uses 1 [OK]
Hint: Use 1 for ascending, -1 for descending in createIndex [OK]
Common Mistakes:
  • Using -1 for ascending index
  • Missing curly braces around field and order
  • Passing field without object braces
3. Given the command db.users.createIndex({ email: 1 }, { unique: true }), what will happen if you try to insert two documents with the same email value?
medium
A. The index will be ignored and duplicates allowed
B. Both documents will be inserted without error
C. The second insert will fail due to duplicate key error
D. MongoDB will automatically update the first document

Solution

  1. Step 1: Understand unique index effect

    Unique index prevents duplicate values in the indexed field.
  2. Step 2: Predict behavior on duplicate insert

    Inserting a duplicate email will cause an error and reject the second insert.
  3. Final Answer:

    The second insert will fail due to duplicate key error -> Option C
  4. Quick Check:

    Unique index blocks duplicates [OK]
Hint: Unique index stops duplicate values in that field [OK]
Common Mistakes:
  • Assuming duplicates are allowed with unique index
  • Thinking MongoDB auto-updates duplicates
  • Believing index is ignored on duplicates
4. What is wrong with this command? db.products.createIndex({ price: 'ascending' })
medium
A. The field name must be a string
B. The collection name is missing
C. createIndex cannot be used on numeric fields
D. The index order must be 1 or -1, not 'ascending'

Solution

  1. Step 1: Check index order values

    MongoDB requires 1 for ascending or -1 for descending, not strings like 'ascending'.
  2. Step 2: Identify error cause

    Using 'ascending' causes syntax error because it's not a valid order value.
  3. Final Answer:

    The index order must be 1 or -1, not 'ascending' -> Option D
  4. Quick Check:

    Index order must be 1 or -1 [OK]
Hint: Use 1 or -1 for index order, not words [OK]
Common Mistakes:
  • Using string values instead of 1 or -1
  • Thinking createIndex can't index numbers
  • Confusing collection name with index fields
5. You want to create a compound index on lastName (ascending) and age (descending) fields, ensuring no two documents have the same combination. Which command is correct?
hard
A. db.collection.createIndex({ lastName: -1, age: 1 }, { unique: false })
B. db.collection.createIndex({ lastName: 1, age: -1 }, { unique: true })
C. db.collection.createIndex({ lastName: 1, age: 1 }, { unique: true })
D. db.collection.createIndex({ lastName: 'asc', age: 'desc' }, { unique: true })

Solution

  1. Step 1: Understand compound index syntax

    Compound index uses an object with multiple fields and their order: 1 for ascending, -1 for descending.
  2. Step 2: Check uniqueness and order

    db.collection.createIndex({ lastName: 1, age: -1 }, { unique: true }) matches ascending lastName (1), descending age (-1), and unique true to prevent duplicates.
  3. Step 3: Verify other options

    db.collection.createIndex({ lastName: -1, age: 1 }, { unique: false }) reverses order and is not unique; C uses ascending age instead of descending; D uses invalid string values.
  4. Final Answer:

    db.collection.createIndex({ lastName: 1, age: -1 }, { unique: true }) -> Option B
  5. Quick Check:

    Compound unique index uses correct order and unique:true [OK]
Hint: Use 1/-1 for order and unique:true for no duplicates [OK]
Common Mistakes:
  • Using strings 'asc'/'desc' instead of 1/-1
  • Setting unique:false when uniqueness is needed
  • Mixing ascending and descending order incorrectly
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