0
0
MongoDBquery~10 mins

Unique index behavior 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 unique index on the "email" field in MongoDB.

MongoDB
db.users.createIndex({ email: 1 }, { unique: [1] })
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
Cunique
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a boolean for the unique option.
Setting unique to false which does not enforce uniqueness.
2fill in blank
medium

Complete the code to insert a document that violates a unique index on "username".

MongoDB
db.users.insertOne({ username: [1], age: 25 })
Drag options to blanks, or click blank then click option'
A"unique"
Bjohn_doe
C25
D"john_doe"
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting string values causing syntax errors.
Using a number instead of a string for username.
3fill in blank
hard

Fix the error in the unique index creation by completing the code.

MongoDB
db.users.createIndex({ email: 1 }, { [1]: true })
Drag options to blanks, or click blank then click option'
AuniqueIndex
Bunique
Cunique_key
Dunique_value
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect option names like uniqueIndex or unique_key.
Misspelling the option name.
4fill in blank
hard

Fill both blanks to create a unique index on "phone" field in descending order.

MongoDB
db.contacts.createIndex({ phone: [1] }, { [2]: true })
Drag options to blanks, or click blank then click option'
A-1
B1
Cunique
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of -1 for descending order.
Using index instead of unique for uniqueness.
5fill in blank
hard

Fill all three blanks to create a unique compound index on "firstName" ascending and "lastName" descending.

MongoDB
db.users.createIndex({ firstName: [1], lastName: [2] }, { [3]: true })
Drag options to blanks, or click blank then click option'
A1
B-1
Cunique
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up ascending and descending values.
Using index instead of unique for uniqueness.