Recall & Review
beginner
What is a unique index in MongoDB?
A unique index in MongoDB ensures that the indexed field does not have duplicate values across documents in a collection. It helps keep data unique and prevents duplicate entries.
Click to reveal answer
beginner
What happens if you try to insert a document with a duplicate value on a unique indexed field?
MongoDB will reject the insert operation and return an error because the unique index prevents duplicate values in that field.
Click to reveal answer
intermediate
Can a unique index be created on multiple fields in MongoDB?
Yes, MongoDB supports unique compound indexes that enforce uniqueness across the combination of multiple fields together, not just one field.
Click to reveal answer
intermediate
How does MongoDB treat documents with missing fields when a unique index is created on that field?
MongoDB allows multiple documents to have missing or null values for a unique indexed field because null is treated as a unique value for each document.
Click to reveal answer
beginner
How can you create a unique index on the 'email' field in a MongoDB collection named 'users'?
Use the command: db.users.createIndex({ email: 1 }, { unique: true }) to create a unique index on the 'email' field.
Click to reveal answer
What does a unique index in MongoDB prevent?
✗ Incorrect
A unique index prevents duplicate values in the indexed field, ensuring each value is unique.
What happens if you insert a document with a duplicate value on a unique indexed field?
✗ Incorrect
MongoDB rejects the insert and returns an error to maintain uniqueness.
Can a unique index be created on multiple fields in MongoDB?
✗ Incorrect
MongoDB supports compound unique indexes that enforce uniqueness across multiple fields combined.
How does MongoDB treat multiple documents missing a field with a unique index on that field?
✗ Incorrect
MongoDB allows multiple documents to have missing or null values for a unique indexed field.
Which command creates a unique index on the 'username' field in the 'accounts' collection?
✗ Incorrect
The correct syntax to create a unique index is createIndex with the option { unique: true }.
Explain what a unique index is in MongoDB and how it affects data insertion.
Think about how MongoDB keeps certain fields unique across documents.
You got /3 concepts.
Describe how MongoDB handles documents with missing or null values when a unique index is applied on that field.
Consider how null values are treated differently than duplicates.
You got /3 concepts.