0
0
MongoDBquery~20 mins

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

Choose your learning style9 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.