Challenge - 5 Problems
Custom _id Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What will be the _id of the inserted document?
Consider the following MongoDB insert operation where a custom _id is provided:
What will be the value of the _id field in the inserted document?
db.users.insertOne({ _id: "user123", name: "Alice" })What will be the value of the _id field in the inserted document?
MongoDB
db.users.insertOne({ _id: "user123", name: "Alice" })Attempts:
2 left
💡 Hint
If you provide _id explicitly, MongoDB uses that value instead of generating one.
✗ Incorrect
When you insert a document with a custom _id, MongoDB uses that exact value as the document's _id instead of creating a new ObjectId.
❓ query_result
intermediate2:00remaining
What happens if you insert two documents with the same custom _id?
Given the following two insert operations:
What will happen when the second insert runs?
db.products.insertOne({ _id: 101, name: "Pen" })
db.products.insertOne({ _id: 101, name: "Pencil" })What will happen when the second insert runs?
MongoDB
db.products.insertOne({ _id: 101, name: "Pen" })
db.products.insertOne({ _id: 101, name: "Pencil" })Attempts:
2 left
💡 Hint
The _id field must be unique in a collection.
✗ Incorrect
MongoDB enforces uniqueness on the _id field. Inserting a document with an existing _id causes a duplicate key error.
📝 Syntax
advanced2:00remaining
Which insert command correctly sets a custom _id as an ObjectId?
You want to insert a document with a custom _id that is an ObjectId value. Which of the following commands is correct?
Attempts:
2 left
💡 Hint
ObjectId needs to be called as a function with a string argument.
✗ Incorrect
Option B correctly calls ObjectId with a string to create an ObjectId value. Option B uses a string literal, not an ObjectId. Option B misses parentheses. Option B uses the ObjectId function itself, not a value.
❓ optimization
advanced2:00remaining
What is a benefit of using custom _id values instead of default ObjectIds?
Which of the following is a valid advantage of using custom _id values in MongoDB documents?
Attempts:
2 left
💡 Hint
Think about how meaningful keys can help when searching or indexing.
✗ Incorrect
Using custom _id values lets you assign meaningful identifiers, which can make queries easier to understand and sometimes improve indexing strategies. MongoDB does not automatically optimize queries better just because _id is custom. Custom _id values do not reduce storage size necessarily, and _id must always be unique.
🧠 Conceptual
expert2:00remaining
Why might using a sequential integer as a custom _id cause performance issues?
Suppose you use sequential integers (1, 2, 3, ...) as custom _id values in a large MongoDB collection. What is a likely downside of this approach?
Attempts:
2 left
💡 Hint
Think about how indexes handle inserts with increasing keys.
✗ Incorrect
Using sequential _id values causes all inserts to target the same end of the index, creating hotspots that can slow down writes. MongoDB supports integer _id values. Documents are stored in insertion order, and duplicates only occur if you reuse _id values.