0
0
MongoDBquery~20 mins

Unique index behavior in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unique Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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" });
AThe second insert will fail with a duplicate key error.
BThe second insert will be ignored silently.
CThe second insert will succeed and create a duplicate email.
DThe second insert will overwrite the first document.
Attempts:
2 left
💡 Hint
Unique indexes prevent duplicate values in the indexed field.
query_result
intermediate
2: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" });
ABoth inserts fail because the unique index requires all documents to have the field.
BThe second insert fails with a duplicate key error.
CBoth inserts succeed because sparse indexes ignore documents missing the indexed field.
DOnly the first insert succeeds; the second is ignored.
Attempts:
2 left
💡 Hint
Sparse indexes only index documents that contain the indexed field.
📝 Syntax
advanced
2: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.
Adb.users.createIndex(["username", "email"], { unique: true });
Bdb.users.createIndex({ username: 1, email: 1 }, { unique: true });
Cdb.users.createIndex({ username: 1 }, { email: 1, unique: true });
Ddb.users.createIndex({ username: 1, email: 1 }, unique = true);
Attempts:
2 left
💡 Hint
The index keys must be in an object with field names and sort order.
🔧 Debug
advanced
2: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 });
AUnique indexes cannot be created on string fields.
BThe syntax of createIndex is incorrect.
CThe database is in read-only mode.
DThe collection contains duplicate usernames, violating the unique index constraint.
Attempts:
2 left
💡 Hint
Unique indexes require all existing data to be unique on the indexed field.
🧠 Conceptual
expert
3: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.
AA unique partial index enforces uniqueness only on documents matching a filter expression; a unique sparse index ignores documents missing the indexed field.
BA unique sparse index enforces uniqueness only on documents matching a filter expression; a unique partial index ignores documents missing the indexed field.
CBoth indexes behave the same and enforce uniqueness on all documents regardless of fields.
DUnique partial indexes allow duplicates; unique sparse indexes do not.
Attempts:
2 left
💡 Hint
Partial indexes use a filter expression; sparse indexes depend on field presence.