Bird
Raised Fist0
MongoDBquery~20 mins

Multikey indexes for arrays in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Multikey Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of a query using a multikey index on an array field

Given a MongoDB collection products with documents like:

{ "_id": 1, "tags": ["red", "cotton", "sale"] }

and a multikey index on tags, what documents will this query return?

db.products.find({ tags: "cotton" })
MongoDB
db.products.insertMany([
  { _id: 1, tags: ["red", "cotton", "sale"] },
  { _id: 2, tags: ["blue", "wool"] },
  { _id: 3, tags: ["cotton", "new"] }
])
db.products.createIndex({ tags: 1 })
db.products.find({ tags: "cotton" })
ADocuments with _id 2 only
BDocuments with _id 1 and 3
CDocuments with _id 1, 2, and 3
DNo documents returned
Attempts:
2 left
💡 Hint

Think about how multikey indexes work with array fields and matching values.

🧠 Conceptual
intermediate
1:30remaining
Understanding multikey index limitations

Which of the following is a limitation of MongoDB multikey indexes on array fields?

AThey cannot index arrays with more than 100 elements
BThey automatically index only the first element of the array
CThey do not support indexing nested arrays (arrays inside arrays)
DThey require the array to be sorted before indexing
Attempts:
2 left
💡 Hint

Think about how MongoDB handles arrays inside arrays for indexing.

📝 Syntax
advanced
1:30remaining
Correct syntax to create a multikey index on an array field

Which of the following commands correctly creates a multikey index on the categories array field in MongoDB?

Adb.collection.createIndex({ categories: { type: "array" } })
Bdb.collection.createIndex({ categories: "multikey" })
Cdb.collection.createIndex({ categories: [1] })
Ddb.collection.createIndex({ categories: 1 })
Attempts:
2 left
💡 Hint

Remember how MongoDB creates indexes on fields, including arrays.

❓ optimization
advanced
2:00remaining
Optimizing queries with multikey indexes on multiple array fields

Consider a collection with documents having two array fields: colors and sizes. You want to efficiently query documents where colors contains "red" and sizes contains "large".

Which index strategy is best for this query?

ACreate a compound multikey index on { colors: 1, sizes: 1 }
BCreate separate single-field multikey indexes on colors and sizes
CCreate a text index on colors and sizes fields
DCreate a hashed index on colors and sizes
Attempts:
2 left
💡 Hint

Think about how compound multikey indexes work with multiple array fields.

🔧 Debug
expert
2:30remaining
Why does this query not use the multikey index?

You created a multikey index on the tags array field. But this query is slow and does not use the index:

db.products.find({ tags: { $all: ["cotton", "sale"] } })

What is the most likely reason?

AThe multikey index exists but MongoDB cannot use it efficiently for $all with multiple elements
BThe query uses $all with multiple values, which requires a special index type
CMultikey indexes do not support $all queries on array fields
DThe index was created on a different field, not tags
Attempts:
2 left
💡 Hint

Consider how MongoDB uses multikey indexes with $all operator.

Practice

(1/5)
1. What is the main purpose of a multikey index in MongoDB when applied to an array field?
easy
A. To index each element inside the array separately for faster queries
B. To store the entire array as a single indexed value
C. To prevent duplicate values inside the array
D. To convert arrays into strings before indexing

Solution

  1. Step 1: Understand what a multikey index does

    A multikey index indexes each element of an array field separately, allowing efficient queries on any element inside the array.
  2. Step 2: Compare options with this behavior

    Only To index each element inside the array separately for faster queries describes indexing each element separately. Other options describe incorrect behaviors.
  3. Final Answer:

    To index each element inside the array separately for faster queries -> Option A
  4. Quick Check:

    Multikey index = indexes array elements separately [OK]
Hint: Multikey indexes each array element, not the whole array [OK]
Common Mistakes:
  • Thinking multikey indexes the whole array as one value
  • Confusing multikey with unique constraints
  • Assuming arrays are converted to strings before indexing
2. Which of the following is the correct syntax to create a multikey index on the tags array field in a MongoDB collection named posts?
easy
A. db.posts.createIndex({ tags: [1] })
B. db.posts.createIndex({ tags: 1 })
C. db.posts.createIndex({ tags: 'multikey' })
D. db.posts.createIndex({ tags: { $each: 1 } })

Solution

  1. Step 1: Recall MongoDB index creation syntax

    To create an index on a field, use createIndex({ field: 1 }) for ascending order.
  2. Step 2: Understand multikey index creation

    MongoDB automatically creates a multikey index if the field is an array. No special keyword is needed.
  3. Final Answer:

    db.posts.createIndex({ tags: 1 }) -> Option B
  4. Quick Check:

    Index array field normally = multikey index [OK]
Hint: Just index the array field normally; MongoDB handles multikey [OK]
Common Mistakes:
  • Using special keywords like 'multikey' in index definition
  • Trying to pass array syntax in index keys
  • Misusing MongoDB operators inside createIndex
3. Given the collection products with documents like { _id: 1, categories: ["electronics", "gadgets"] }, what will be the result of the query db.products.find({ categories: "gadgets" }) if a multikey index exists on categories?
medium
A. [{ _id: 1, categories: ["electronics"] }]
B. [] (empty array)
C. Error: Cannot query array field
D. [{ _id: 1, categories: ["electronics", "gadgets"] }]

Solution

  1. Step 1: Understand query on array field with multikey index

    The query searches for documents where the array contains "gadgets". The multikey index allows fast lookup of such elements.
  2. Step 2: Check the document matching the query

    The document with _id 1 has "gadgets" in its categories array, so it matches and will be returned.
  3. Final Answer:

    [{ _id: 1, categories: ["electronics", "gadgets"] }] -> Option D
  4. Quick Check:

    Query array element with multikey index returns matching docs [OK]
Hint: Query matches docs with array containing the value [OK]
Common Mistakes:
  • Expecting empty result because array is indexed
  • Thinking query errors on array fields
  • Confusing partial array matches with exact matches
4. You created a multikey index on the tags array field, but your query db.articles.find({ tags: "news" }) is slow. What is a likely reason?
medium
A. The index was created on a different field, not tags
B. Multikey indexes do not support queries on array elements
C. You must use $elemMatch to query array fields
D. MongoDB does not index array fields automatically

Solution

  1. Step 1: Check index field correctness

    If the index is on a different field, queries on tags won't use the index, causing slowness.
  2. Step 2: Verify multikey index behavior

    Multikey indexes do support queries on array elements without special operators like $elemMatch.
  3. Final Answer:

    The index was created on a different field, not tags -> Option A
  4. Quick Check:

    Index on correct field = fast query [OK]
Hint: Ensure index is on the queried array field [OK]
Common Mistakes:
  • Believing multikey indexes don't support element queries
  • Thinking $elemMatch is always required
  • Assuming MongoDB indexes arrays automatically without createIndex
5. You have a collection users with documents containing an array field roles. You want to create a multikey index on roles but only for users who are active (active: true). Which is the correct way to create a partial multikey index for this?
hard
A. db.users.createIndex({ roles: 1 }, { condition: { active: true } })
B. db.users.createIndex({ roles: 1, active: true })
C. db.users.createIndex({ roles: 1 }, { partialFilterExpression: { active: true } })
D. db.users.createIndex({ roles: 1 }, { filter: { active: true } })

Solution

  1. Step 1: Understand partial indexes in MongoDB

    Partial indexes use partialFilterExpression to index only documents matching a filter.
  2. Step 2: Apply partialFilterExpression with multikey index

    Creating an index on roles with partialFilterExpression: { active: true } indexes only active users' roles.
  3. Final Answer:

    db.users.createIndex({ roles: 1 }, { partialFilterExpression: { active: true } }) -> Option C
  4. Quick Check:

    Use partialFilterExpression for conditional multikey index [OK]
Hint: Use partialFilterExpression option for conditional indexes [OK]
Common Mistakes:
  • Using invalid options like filter or condition
  • Trying to include filter in index key object
  • Not knowing partialFilterExpression syntax
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