0
0
MongoDBquery~5 mins

Unique index behavior in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AIndexes on multiple fields
BNull values in the indexed field
CAll documents from being inserted
DDuplicate values in the indexed field
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
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
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
Which command creates a unique index on the 'username' field in the 'accounts' collection?
Adb.accounts.createIndex({ username: 1 }, { unique: true })
Bdb.accounts.createIndex({ username: 1 })
Cdb.accounts.ensureIndex({ username: 1 }, true)
Ddb.accounts.createUniqueIndex({ username: 1 })
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.