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 purpose of the _id field in MongoDB documents?
The _id field uniquely identifies each document in a MongoDB collection. It acts like a primary key to ensure no two documents have the same identifier.
Click to reveal answer
beginner
What happens if you insert a document into MongoDB without specifying an _id field?
MongoDB automatically generates a unique _id of type ObjectId for the document before inserting it.
Click to reveal answer
intermediate
Describe the structure of the default ObjectId generated for the _id field.
The ObjectId is a 12-byte value consisting of: a 4-byte timestamp (seconds since epoch), a 5-byte random value, and a 3-byte incrementing counter. This ensures uniqueness and time ordering.
Click to reveal answer
beginner
Can you insert a document with a custom _id value in MongoDB?
Yes, you can specify your own _id value when inserting a document. MongoDB will use it as the unique identifier instead of generating one.
Click to reveal answer
beginner
What error occurs if you try to insert two documents with the same _id value?
MongoDB throws a duplicate key error because _id must be unique within a collection.
Click to reveal answer
What type does MongoDB use by default for the auto-generated _id field?
AInteger
BString
CUUID
DObjectId
✗ Incorrect
MongoDB uses ObjectId as the default type for auto-generated _id values.
If you insert a document without an _id, what does MongoDB do?
AGenerates a unique <code>_id</code> automatically
BRejects the insert
CInserts with a null <code>_id</code>
DAsks the user to provide <code>_id</code>
✗ Incorrect
MongoDB automatically generates a unique _id if none is provided.
Which part of the ObjectId encodes the creation time?
ALast 3 bytes
BFirst 4 bytes
CMiddle 5 bytes
DEntire 12 bytes
✗ Incorrect
The first 4 bytes of ObjectId represent the timestamp of creation.
Can you use a string as a custom _id in MongoDB?
AOnly integers are allowed
BNo, only ObjectId is allowed
CYes, any unique value can be used
DOnly UUIDs are allowed
✗ Incorrect
MongoDB allows any unique value type as a custom _id, including strings.
What happens if you try to insert two documents with the same _id?
AMongoDB throws a duplicate key error
BThe second insert overwrites the first
CMongoDB ignores the second insert
DBoth documents are inserted
✗ Incorrect
MongoDB enforces uniqueness on _id and throws an error on duplicates.
Explain how MongoDB generates the _id field automatically when it is not provided.
Think about the parts that make ObjectId unique and time-ordered.
You got /5 concepts.
Describe what happens if you insert a document with a custom _id that already exists in the collection.
Consider MongoDB's rule about unique identifiers.
You got /3 concepts.
Practice
(1/5)
1. In MongoDB, what happens if you insert a document without specifying the _id field?
easy
A. MongoDB automatically generates a unique _id for the document.
B. The insert operation fails with an error.
C. The document is inserted with a null _id.
D. MongoDB assigns a sequential integer as the _id.
Solution
Step 1: Understand MongoDB's default behavior for _id
MongoDB requires each document to have a unique _id. If not provided, it creates one automatically.
Step 2: Identify the type of auto-generated _id
The auto-generated _id is an ObjectId, which is unique and generated by MongoDB.
Final Answer:
MongoDB automatically generates a unique _id for the document. -> Option A
Quick Check:
Auto-generated _id = unique ObjectId [OK]
Hint: If no _id, MongoDB creates a unique one automatically [OK]
Common Mistakes:
Thinking insert fails without _id
Assuming _id can be null
Believing _id is a simple number
2. Which of the following is the correct way to insert a document without specifying _id in MongoDB shell?
easy
A. db.collection.insertOne({_id: 1, name: 'Alice'})
B. db.collection.insertOne({_id: null, name: 'Alice'})
C. db.collection.insertOne({name: 'Alice'})
D. db.collection.insertOne()
Solution
Step 1: Check syntax for inserting a document without _id
The correct syntax is to provide the document fields except _id, so MongoDB generates it.
Step 2: Evaluate each option
db.collection.insertOne({name: 'Alice'}) inserts a document with only the name field, letting MongoDB create _id. db.collection.insertOne({_id: null, name: 'Alice'}) sets _id to null which is invalid. db.collection.insertOne({_id: 1, name: 'Alice'}) sets _id manually. db.collection.insertOne() is missing the document argument.
Final Answer:
db.collection.insertOne({name: 'Alice'}) -> Option C
Quick Check:
Insert without _id uses document only [OK]
Hint: Insert document without _id to auto-generate it [OK]
A. Second insert overwrites the first document silently.
B. Both inserts succeed; MongoDB allows duplicate _id.
C. First insert fails; _id must be ObjectId.
D. Second insert fails due to duplicate _id; fix by using unique _id values.
Solution
Step 1: Understand _id uniqueness constraint
MongoDB requires _id to be unique in a collection. Duplicate _id values cause insert failure.
Step 2: Analyze the inserts
The first insert with _id: 1 succeeds. The second insert tries the same _id, causing a duplicate key error.
Final Answer:
Second insert fails due to duplicate _id; fix by using unique _id values. -> Option D
Quick Check:
Duplicate _id causes insert failure [OK]
Hint: Each _id must be unique to avoid insert errors [OK]
Common Mistakes:
Thinking MongoDB allows duplicate _id
Assuming _id must be ObjectId type
Believing second insert overwrites first
5. You want to insert multiple documents into a MongoDB collection, but ensure each document has a unique _id without manually specifying it. Which approach correctly achieves this and why?
A. Insert documents without _id but create a unique index on name.
B. Insert documents as is; MongoDB auto-generates unique _id for each document.
C. Manually assign sequential integers as _id before insert.
D. Add _id: null to each document to let MongoDB generate _id.
Solution
Step 1: Understand MongoDB's auto-generation of _id
When documents lack _id, MongoDB automatically creates a unique ObjectId for each during insert.
Step 2: Evaluate each option
Insert documents as is; MongoDB auto-generates unique _id for each document. correctly relies on MongoDB's default behavior. Add _id: null to each document to let MongoDB generate _id. is invalid because _id: null is not allowed. Manually assign sequential integers as _id before insert. requires manual work and risks duplicates. Insert documents without _id but create a unique index on name. creates a unique index on name, unrelated to _id uniqueness.
Final Answer:
Insert documents as is; MongoDB auto-generates unique _id for each document. -> Option B
Quick Check:
Missing _id means MongoDB creates unique ObjectId [OK]
Hint: Insert without _id to get unique ObjectId automatically [OK]