Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the default type of the _id field in MongoDB documents?
The default _id field in MongoDB is an ObjectId, a 12-byte unique identifier automatically generated for each document.
Click to reveal answer
beginner
Can you assign a custom value to the _id field in MongoDB?
Yes, you can assign any unique value to the _id field when inserting a document, such as a string, number, or ObjectId, as long as it is unique within the collection.
Click to reveal answer
intermediate
Why might you want to use a custom _id value instead of the default ObjectId?
Using a custom _id can help you use meaningful identifiers like usernames or codes, avoid extra lookups, or integrate with existing systems that have their own unique IDs.
Click to reveal answer
beginner
What happens if you insert two documents with the same custom _id value?
MongoDB will reject the second insert with a duplicate key error because _id must be unique in a collection.
Click to reveal answer
beginner
How do you insert a document with a custom _id in MongoDB using the shell?
Use the insert command with the _id field set to your custom value, for example:
{ _id: "user123", name: "Alice" }
Click to reveal answer
What type of value can the _id field have in MongoDB?
AOnly ObjectId
BAny unique value
COnly strings
DOnly numbers
✗ Incorrect
The _id field can be any unique value, not just ObjectId.
What happens if you insert a document without specifying _id?
AMongoDB automatically creates an ObjectId for <code>_id</code>
BMongoDB rejects the insert
CThe document has no <code>_id</code>
DYou must specify <code>_id</code> manually
✗ Incorrect
MongoDB automatically generates an ObjectId for _id if you don't specify one.
Why is the _id field important in MongoDB?
AIt stores the document's creation date
BIt is optional and has no special role
CIt uniquely identifies each document
DIt stores the document's size
✗ Incorrect
_id uniquely identifies each document in a collection.
If you want to use a username as the _id, what must you ensure?
AUsernames are unique in the collection
BUsernames are always numbers
CUsernames are ObjectIds
DUsernames are optional
✗ Incorrect
The _id must be unique, so usernames used as _id must be unique.
What error occurs if you insert a document with a duplicate _id?
ASyntax error
BNo error, it overwrites
CConnection error
DDuplicate key error
✗ Incorrect
MongoDB throws a duplicate key error if _id is not unique.
Explain how and why you might use a custom _id value in MongoDB.
Think about identifiers you already use in real life, like usernames or codes.
You got /4 concepts.
Describe what happens if you try to insert two documents with the same _id in MongoDB.
Consider the role of <code>_id</code> as a unique identifier.
You got /3 concepts.
Practice
(1/5)
1.
What is the purpose of the _id field in a MongoDB document?
easy
A. It uniquely identifies each document in a collection.
B. It stores the creation date of the document.
C. It holds the user's login information.
D. It contains the document's size in bytes.
Solution
Step 1: Understand the role of _id in MongoDB
The _id field is a unique identifier for each document in a collection, ensuring no two documents share the same _id.
Step 2: Compare with other options
Other options describe unrelated fields or metadata, not the unique identifier role.
Final Answer:
It uniquely identifies each document in a collection. -> Option A
Quick Check:
_id = unique document ID [OK]
Hint: Remember: _id means unique ID for each document [OK]
Common Mistakes:
Thinking _id stores creation date
Confusing _id with user data fields
Assuming _id is optional
2.
Which of the following is the correct way to insert a document with a custom _id value in MongoDB?
db.users.insertOne({ _id: 123, name: "Alice" })
easy
A. db.users.insertOne({ id: 123, name: "Alice" })
B. db.users.insertOne({ _id: 123, name: "Alice" })
C. db.users.insertOne({ _id: "name", name: "Alice" })
D. db.users.insertOne({ _id: ObjectId(), name: "Alice" })
Solution
Step 1: Identify correct _id field usage
The _id field must be named exactly _id to set a custom ID. db.users.insertOne({ _id: 123, name: "Alice" }) uses _id: 123 correctly.
Step 2: Check other options for errors
db.users.insertOne({ id: 123, name: "Alice" }) uses id instead of _id. db.users.insertOne({ _id: "name", name: "Alice" }) uses a string "name" which is valid but less meaningful here. db.users.insertOne({ _id: ObjectId(), name: "Alice" }) uses default ObjectId, not custom.
Final Answer:
db.users.insertOne({ _id: 123, name: "Alice" }) -> Option B
Quick Check:
Custom _id needs exact field name [OK]
Hint: Use exact field name _id for custom IDs [OK]
Common Mistakes:
Using id instead of _id
Confusing ObjectId() with custom values
Using invalid types for _id
3.
Given the following documents inserted into a collection:
A. SyntaxError due to missing quotes around _id value.
B. TypeError because _id must be a string.
C. No error; both documents inserted successfully.
D. DuplicateKeyError because _id must be unique.
Solution
Step 1: Check uniqueness requirement of _id
The _id field must be unique in a collection. Both documents use _id: 101.
Step 2: Identify the error caused by duplicate _id
Inserting the second document with the same _id causes a DuplicateKeyError.
Final Answer:
DuplicateKeyError because _id must be unique. -> Option D
Quick Check:
Duplicate _id causes insertion error [OK]
Hint: No two documents can share the same _id [OK]
Common Mistakes:
Thinking _id can repeat
Confusing syntax error with duplicate key error
Assuming _id must be string only
5.
You want to store user profiles with custom _id values based on their email addresses to speed up lookups. Which approach is best?
// Option A
{ _id: ObjectId(), email: "user@example.com", name: "User" }
// Option B
{ _id: "user@example.com", name: "User" }
// Option C
{ email: "user@example.com", name: "User" }
// Option D
{ _id: UUID(), email: "user@example.com", name: "User" }
hard
A. Use default ObjectId and store email separately.
B. Do not use _id, just store email field.
C. Set _id to the email string for direct lookup.
D. Use UUID as _id and email separately.
Solution
Step 1: Understand the goal of custom _id
The goal is to speed up lookups by using email as the unique identifier.
Step 2: Evaluate options for best fit
Setting _id to the email string enables direct lookup using the email as _id, making queries fast. Using default ObjectId or UUID with separate email requires additional indexing. Omitting _id is invalid since it is mandatory.
Final Answer:
Set _id to the email string for direct lookup. -> Option C
Quick Check:
Custom _id as email speeds queries [OK]
Hint: Use email as _id for fast direct lookups [OK]