0
0
MongoDBquery~10 mins

Sparse indexes 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 sparse index on the field "email".

MongoDB
db.users.createIndex({ email: 1 }, { [1]: true })
Drag options to blanks, or click blank then click option'
Asparse
Bbackground
Cunique
DexpireAfterSeconds
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'unique' instead of 'sparse' causes errors if duplicates exist.
Omitting the sparse option creates a normal index including all documents.
2fill in blank
medium

Complete the code to find documents where the "phone" field exists using the sparse index.

MongoDB
db.contacts.find({ [1]: { $exists: true } })
Drag options to blanks, or click blank then click option'
Aphone
Bemail
Caddress
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Querying on a different field than the sparse index.
Using $exists: false instead of true.
3fill in blank
hard

Fix the error in the code to create a sparse index on "username".

MongoDB
db.accounts.createIndex({ username: 1 }, { [1]: true })
Drag options to blanks, or click blank then click option'
Aunique
BexpireAfterSeconds
Cbackground
Dsparse
Attempts:
3 left
💡 Hint
Common Mistakes
Using string 'true' instead of boolean true.
Using an unrelated option name.
4fill in blank
hard

Fill both blanks to create an index on "profile.age" and make it run in the background.

MongoDB
db.users.createIndex({ [1]: 1 }, { [2]: true })
Drag options to blanks, or click blank then click option'
Aprofile.age
Bsparse
Cbackground
Dunique
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sparse' instead of 'background' for the second option.
Using 'unique' which is unrelated here.
5fill in blank
hard

Fill all three blanks to create a sparse, unique index on "email" with background creation.

MongoDB
db.customers.createIndex({ [1]: 1 }, { [2]: true, [3]: true, [4]: true })
Drag options to blanks, or click blank then click option'
Aemail
Bunique
Csparse
Dbackground
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing sparse and unique options.
Omitting background option.