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?
AIndexes on multiple fields
BNull values in the indexed field
CAll documents from being inserted
DDuplicate values in the indexed field
✗ 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?
AMongoDB returns an error and rejects the insert
BThe duplicate is ignored silently
CMongoDB updates the existing document
DThe insert succeeds without error
✗ Incorrect
MongoDB rejects the insert and returns an error to maintain uniqueness.
Can a unique index be created on multiple fields in MongoDB?
ANo, only single fields can have unique indexes
BYes, using a compound unique index
CYes, but only if fields are numeric
DNo, MongoDB does not support unique indexes
✗ 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?
AIt rejects all but one document
BIt treats missing fields as duplicates
CIt allows multiple documents with missing or null values
DIt converts missing fields to empty strings
✗ 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?
Hint: Use createIndex with { unique: true } to enforce uniqueness [OK]
Common Mistakes:
Using wrong option name like 'duplicate'
Using non-existent methods like createUniqueIndex
Setting unique to false by mistake
3. Given a collection with a unique index on username, what happens if you run this insert twice?
db.users.insertOne({ username: "alice" })
medium
A. Both inserts succeed, creating two identical documents
B. The second insert will fail with a duplicate key error
C. The second insert overwrites the first document
D. MongoDB ignores the unique index and inserts both
Solution
Step 1: Understand unique index effect on inserts
A unique index on username prevents duplicate values in that field.
Step 2: Predict behavior on duplicate insert
The first insert succeeds. The second insert with the same username causes a duplicate key error and fails.
Final Answer:
The second insert will fail with a duplicate key error -> Option B
Quick Check:
Duplicate insert with unique index = error [OK]
Hint: Duplicate insert on unique field causes error [OK]
Common Mistakes:
Assuming duplicates are allowed
Thinking second insert overwrites first
Believing MongoDB ignores unique index
4. You created a unique index on email but still see duplicate emails in your collection. What is the most likely cause?
medium
A. The unique index was created after duplicates already existed
B. The unique index was created with the wrong field name
C. MongoDB does not enforce uniqueness on indexes
D. The collection is empty
Solution
Step 1: Understand unique index creation behavior
If duplicates exist before creating a unique index, MongoDB will fail to create it unless duplicates are removed or ignored.
Step 2: Analyze why duplicates remain
If the index was forced or created without validation, duplicates remain. The unique index was created after duplicates already existed explains this scenario. The unique index was created with the wrong field name would cause index creation failure. MongoDB does not enforce uniqueness on indexes is false. The collection is empty is irrelevant.
Final Answer:
The unique index was created after duplicates already existed -> Option A
Quick Check:
Existing duplicates block unique index enforcement [OK]
Hint: Create unique index before duplicates exist [OK]
Common Mistakes:
Assuming MongoDB ignores duplicates silently
Not checking field name correctness
Thinking empty collection causes duplicates
5. You want to ensure uniqueness on user.email in documents where user.active is true only. How can you create a unique index for this condition?
hard
A. Create a compound unique index on user.email and user.active without filter
B. Create a unique index on user.email without any filter
C. Create a unique index on user.active only
D. Create a partial unique index with filter { 'user.active': true } on field user.email
Solution
Step 1: Understand partial unique index
A partial unique index applies uniqueness only to documents matching a filter condition.
Step 2: Apply partial index to user.email where user.active is true
Create a partial unique index with filter { 'user.active': true } on field user.email correctly creates a partial unique index on user.email with filter { 'user.active': true }. Create a unique index on user.email without any filter enforces uniqueness on all documents. Create a compound unique index on user.email and user.active without filter enforces uniqueness on combined fields but not conditional. Create a unique index on user.active only is unrelated.
Final Answer:
Create a partial unique index with filter { 'user.active': true } on field user.email -> Option D
Quick Check:
Partial unique index = conditional uniqueness [OK]
Hint: Use partial unique index with filter for conditional uniqueness [OK]
Common Mistakes:
Creating unique index without filter for conditional uniqueness
Using compound index without filter
Indexing wrong field only
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.