A multikey index is an index that MongoDB creates on fields that hold arrays. It indexes each element of the array separately, allowing efficient queries on array contents.
Click to reveal answer
beginner
How does MongoDB handle indexing when a document field contains an array?
MongoDB creates a multikey index by indexing each element of the array as if it were a separate value, enabling queries to match any element inside the array.
Click to reveal answer
intermediate
Can a multikey index be created on multiple array fields in the same document?
No, MongoDB does not support multikey indexes on multiple array fields in the same document because it would create a combinatorial explosion of index entries.
Click to reveal answer
beginner
What is a practical example of a query that benefits from a multikey index?
If a collection has documents with a field 'tags' that is an array, a query like { tags: 'mongodb' } can quickly find documents containing 'mongodb' in the tags array using a multikey index.
Click to reveal answer
intermediate
What happens if you create a multikey index on a field that sometimes contains arrays and sometimes single values?
MongoDB treats single values as arrays with one element and indexes them the same way, so the multikey index works seamlessly for both cases.
Click to reveal answer
What does a multikey index in MongoDB index?
AOnly the first element of an array
BThe entire array as a single value
CEach element of an array field separately
DOnly non-array fields
✗ Incorrect
A multikey index indexes each element of an array field separately to allow efficient queries on any array element.
Can MongoDB create a multikey index on two different array fields in the same document?
AYes, always
BNo, it is not supported
COnly if arrays have the same length
DOnly in MongoDB versions before 4.0
✗ Incorrect
MongoDB does not support multikey indexes on multiple array fields in the same document to avoid combinatorial index explosion.
If a field contains a single value instead of an array, how does a multikey index treat it?
AIndexes it as a single-element array
BIgnores the value
CThrows an error
DIndexes only if it is a string
✗ Incorrect
MongoDB treats single values as arrays with one element when creating a multikey index.
Which query benefits most from a multikey index on an array field 'tags'?
A{ "tags": { "$size": 3 } }
B{ "tags": ["mongodb", "database"] }
C{ "tags": { "$exists": true } }
D{ "tags": "mongodb" }
✗ Incorrect
A query searching for a specific element in the array, like { tags: 'mongodb' }, uses the multikey index efficiently.
What is a limitation of multikey indexes in MongoDB?
AThey cannot index multiple array fields in the same document
BThey cannot be used on array fields
CThey cannot index nested documents
DThey slow down all queries
✗ Incorrect
Multikey indexes cannot be created on multiple array fields in the same document due to index complexity.
Explain what a multikey index is and how it works with array fields in MongoDB.
Think about how MongoDB handles arrays when creating indexes.
You got /3 concepts.
Describe a scenario where a multikey index would improve query performance and a limitation to be aware of.
Consider both benefits and restrictions of multikey indexes.
You got /3 concepts.
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
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.
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.
Final Answer:
To index each element inside the array separately for faster queries -> Option A
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
Step 1: Recall MongoDB index creation syntax
To create an index on a field, use createIndex({ field: 1 }) for ascending order.
Step 2: Understand multikey index creation
MongoDB automatically creates a multikey index if the field is an array. No special keyword is needed.
Final Answer:
db.posts.createIndex({ tags: 1 }) -> Option B
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
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.
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.
Final Answer:
[{ _id: 1, categories: ["electronics", "gadgets"] }] -> Option D
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
Step 1: Check index field correctness
If the index is on a different field, queries on tags won't use the index, causing slowness.
Step 2: Verify multikey index behavior
Multikey indexes do support queries on array elements without special operators like $elemMatch.
Final Answer:
The index was created on a different field, not tags -> Option A
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?