0
0
MongoDBquery~10 mins

Multikey indexes for arrays in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a multikey index on the 'tags' array field.

MongoDB
db.articles.createIndex({ tags: [1] })
Drag options to blanks, or click blank then click option'
A"text"
B-1
C1
D"hashed"
Attempts:
3 left
💡 Hint
Common Mistakes
Using -1 instead of 1 creates a descending index but still works; however, 1 is standard.
Using 'text' or 'hashed' is for different index types, not multikey.
2fill in blank
medium

Complete the code to find documents where the 'tags' array contains 'mongodb'.

MongoDB
db.articles.find({ tags: [1] })
Drag options to blanks, or click blank then click option'
A{ $all: ["mongodb"] }
B"mongodb"
C{ $elemMatch: { $eq: "mongodb" } }
D{ $in: ["mongodb"] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using $all or $in works but is more complex than needed for a single value.
Using $elemMatch is unnecessary for a simple equality check.
3fill in blank
hard

Fix the error in the index creation code to correctly create a multikey index on 'comments.author'.

MongoDB
db.posts.createIndex({ [1]: 1 })
Drag options to blanks, or click blank then click option'
A"comments_authors"
B"author"
C"comments"
D"comments.author"
Attempts:
3 left
💡 Hint
Common Mistakes
Indexing just 'comments' creates an index on the array but not on the nested field.
Using 'author' alone misses the array context.
Using 'comments_authors' is not a valid field name.
4fill in blank
hard

Fill both blanks to create a compound multikey index on 'tags' and 'comments.author'.

MongoDB
db.posts.createIndex({ [1]: 1, [2]: 1 })
Drag options to blanks, or click blank then click option'
A"tags"
B"comments.author"
C"author"
D"comments"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'author' alone misses the array context.
Using 'comments' alone does not index the nested field.
Mixing up field names causes errors.
5fill in blank
hard

Fill all three blanks to create a multikey index on 'categories', 'tags', and 'comments.author'.

MongoDB
db.blog.createIndex({ [1]: 1, [2]: 1, [3]: 1 })
Drag options to blanks, or click blank then click option'
A"categories"
B"tags"
C"comments.author"
D"author"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'author' alone instead of 'comments.author'.
Omitting any array field from the index.
Using incorrect field names.