Challenge - 5 Problems
Unique Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What happens when inserting duplicate values on a unique index?
Consider a MongoDB collection with a unique index on the field
email. What will be the result of inserting a document with an email value that already exists in the collection?MongoDB
db.users.createIndex({ email: 1 }, { unique: true });
db.users.insertOne({ name: "Alice", email: "alice@example.com" });
db.users.insertOne({ name: "Bob", email: "alice@example.com" });Attempts:
2 left
💡 Hint
Unique indexes prevent duplicate values in the indexed field.
✗ Incorrect
A unique index in MongoDB enforces uniqueness for the indexed field. Attempting to insert a document with a duplicate value in that field causes a duplicate key error.
❓ query_result
intermediate2:00remaining
What is the output of this query with a unique sparse index?
A collection has a unique sparse index on the field
phone. The collection contains documents with and without the phone field. What will be the result of inserting two documents without the phone field?MongoDB
db.contacts.createIndex({ phone: 1 }, { unique: true, sparse: true });
db.contacts.insertOne({ name: "John" });
db.contacts.insertOne({ name: "Jane" });Attempts:
2 left
💡 Hint
Sparse indexes only index documents that contain the indexed field.
✗ Incorrect
A unique sparse index only enforces uniqueness on documents that have the indexed field. Documents missing the field are not indexed and can be duplicated.
📝 Syntax
advanced2:00remaining
Which command correctly creates a unique compound index on fields
username and email?Select the valid MongoDB command to create a unique compound index on
username and email fields.Attempts:
2 left
💡 Hint
The index keys must be in an object with field names and sort order.
✗ Incorrect
The correct syntax uses an object with field names and sort order, and the options object with unique: true.
🔧 Debug
advanced2:00remaining
Why does this unique index creation fail?
You try to create a unique index on the
username field, but MongoDB returns an error. The collection already has documents with duplicate username values. What is the cause of the failure?MongoDB
db.users.createIndex({ username: 1 }, { unique: true });Attempts:
2 left
💡 Hint
Unique indexes require all existing data to be unique on the indexed field.
✗ Incorrect
MongoDB cannot create a unique index if existing documents violate uniqueness. The duplicates must be removed or fixed first.
🧠 Conceptual
expert3:00remaining
How does a unique partial index differ from a unique sparse index in MongoDB?
Choose the correct statement about the difference between a unique partial index and a unique sparse index.
Attempts:
2 left
💡 Hint
Partial indexes use a filter expression; sparse indexes depend on field presence.
✗ Incorrect
Partial indexes apply a filter to select documents to index, enforcing uniqueness only on those. Sparse indexes index only documents with the indexed field present.