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 an ObjectId in MongoDB?
An ObjectId is a unique identifier automatically generated by MongoDB for each document. It ensures every document has a unique ID.
Click to reveal answer
beginner
How many bytes long is an ObjectId?
An ObjectId is 12 bytes long, combining different pieces of information to make it unique.
Click to reveal answer
intermediate
What are the parts that make up an ObjectId?
An ObjectId consists of: 4 bytes of timestamp, 5 bytes of random value (usually machine identifier and process id), and 3 bytes of an incrementing counter.
Click to reveal answer
beginner
Why does ObjectId include a timestamp?
The timestamp helps to know when the document was created and ensures part of the ID is unique over time.
Click to reveal answer
intermediate
Can ObjectId be manually created or changed?
Yes, you can create or assign your own ObjectId, but MongoDB generates it automatically if you don't provide one.
Click to reveal answer
How many bytes does a MongoDB ObjectId contain?
A12 bytes
B16 bytes
C8 bytes
D24 bytes
✗ Incorrect
An ObjectId is exactly 12 bytes long, combining timestamp, machine/process info, and a counter.
Which part of the ObjectId helps identify when the document was created?
AThe entire 12 bytes equally
BThe 5-byte random value
CThe 4-byte timestamp
DThe 3-byte counter
✗ Incorrect
The first 4 bytes represent the timestamp of creation.
What ensures ObjectId uniqueness across different machines?
A5-byte random value (machine and process id)
B4-byte timestamp
C3-byte counter
DUser input
✗ Incorrect
The 5-byte random value includes machine and process identifiers to avoid collisions.
Can you assign your own ObjectId to a MongoDB document?
AOnly if the document is empty
BNo, MongoDB only generates ObjectIds automatically
COnly in older MongoDB versions
DYes, you can manually create and assign it
✗ Incorrect
You can manually create and assign ObjectIds if you want, but MongoDB generates them by default.
What is the purpose of the 3-byte counter in ObjectId?
ATo store the document size
BTo ensure uniqueness when multiple ObjectIds are generated in the same second
CTo record the user ID
DTo hold the database name
✗ Incorrect
The 3-byte counter increments to avoid duplicates when many ObjectIds are created quickly.
Explain what an ObjectId is and describe its components.
Think about how MongoDB ensures each document has a unique ID.
You got /5 concepts.
Why does MongoDB include a timestamp in the ObjectId? How does this help?
Consider what information the timestamp gives about the document.
You got /3 concepts.
Practice
(1/5)
1. What does a MongoDB ObjectId primarily represent?
easy
A. A random number generated by the client
B. A unique identifier for documents in a collection
C. A user's login session ID
D. A timestamp of when the database was created
Solution
Step 1: Understand the purpose of ObjectId
ObjectId is designed to uniquely identify each document in a MongoDB collection.
Step 2: Differentiate from other identifiers
It is not a session ID, random number, or database creation timestamp but a unique document ID.
Final Answer:
A unique identifier for documents in a collection -> Option B
Quick Check:
ObjectId = Unique document ID [OK]
Hint: ObjectId is always a unique document ID in MongoDB [OK]
Common Mistakes:
Thinking ObjectId is a random number
Confusing ObjectId with session or user IDs
Assuming it stores database creation time
2. Which of the following is the correct way to create a new ObjectId in MongoDB using the shell?
easy
A. ObjectId()
B. ObjectId.new()
C. new ObjectId()
D. create ObjectId()
Solution
Step 1: Recall MongoDB shell syntax
In the MongoDB shell, new ObjectId() is used to create a new ObjectId instance.
Step 2: Check other options for syntax errors
Options B, C, and D use incorrect syntax or are invalid in MongoDB shell.
Final Answer:
new ObjectId() -> Option C
Quick Check:
Use new ObjectId() to create new IDs [OK]
Hint: Use new ObjectId() in MongoDB shell [OK]
Common Mistakes:
Using ObjectId() without new keyword
Trying to call ObjectId as a method
Using create keyword which is invalid
3. Given the following code snippet in MongoDB shell:
var id = ObjectId(); var timestamp = id.getTimestamp(); print(timestamp);
What does timestamp represent?
medium
A. The creation time of the ObjectId
B. The current system time when print runs
C. The last modified time of the document
D. The expiration time of the ObjectId
Solution
Step 1: Understand ObjectId structure
ObjectId contains a timestamp of when it was created embedded in its first 4 bytes.
Step 2: Explain getTimestamp() method
The getTimestamp() method extracts this creation time from the ObjectId.
Final Answer:
The creation time of the ObjectId -> Option A
Quick Check:
getTimestamp() = ObjectId creation time [OK]
Hint: getTimestamp() returns ObjectId creation time [OK]
Common Mistakes:
Assuming it returns current time
Confusing with document modification time
Thinking it returns expiration time
4. You wrote this code in MongoDB shell:
var id = ObjectId.getTimestamp();
But it throws an error. What is the problem?
medium
A. getTimestamp() is not a static method of ObjectId
B. ObjectId is not defined in MongoDB shell
C. You must pass an argument to getTimestamp()
D. ObjectId.getTimestamp() returns a promise and needs await
Solution
Step 1: Check method usage
getTimestamp() is an instance method, not a static method on ObjectId class.
Step 2: Correct usage
You must create an ObjectId instance first, then call id.getTimestamp().
Final Answer:
getTimestamp() is not a static method of ObjectId -> Option A
Quick Check:
getTimestamp() needs ObjectId instance [OK]
Hint: Call getTimestamp() on ObjectId instance, not class [OK]
Common Mistakes:
Calling getTimestamp() directly on ObjectId
Forgetting to create ObjectId instance
Expecting getTimestamp() to be async
5. You want to generate a MongoDB ObjectId that corresponds to a specific timestamp (e.g., Jan 1, 2020). Which approach is correct?
hard
A. Create an ObjectId with the timestamp bytes set, then fill remaining bytes with zeros
B. Use ObjectId() constructor with a date string argument
C. Manually convert the date to hex and concatenate with random bytes
D. Use ObjectId.createFromTime(timestampInSeconds)
Solution
Step 1: Understand ObjectId creation from timestamp
MongoDB provides ObjectId.createFromTime() to create an ObjectId from a Unix timestamp in seconds.
Step 2: Evaluate other options
Options C and D are manual and error-prone; B is invalid as ObjectId constructor does not accept date strings.
Final Answer:
Use ObjectId.createFromTime(timestampInSeconds) -> Option D
Quick Check:
createFromTime() creates ObjectId from timestamp [OK]
Hint: Use createFromTime() to make ObjectId from timestamp [OK]