0
0
MongoDBquery~20 mins

Auto-generated _id behavior in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB _id Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the type of the default _id field in MongoDB?
When you insert a document into a MongoDB collection without specifying the _id field, MongoDB automatically generates one. What is the data type of this auto-generated _id?
AAn ObjectId, a 12-byte BSON type
BA string representing a UUID
CAn integer starting from 1 and incrementing
DA timestamp value only
Attempts:
2 left
💡 Hint
Think about the unique identifier MongoDB uses internally for documents.
query_result
intermediate
2:00remaining
What happens if you insert two documents with the same _id value?
Consider inserting two documents into a MongoDB collection where both documents have the same _id value explicitly set. What will happen?
ABoth documents will be inserted successfully with duplicate _id values.
BThe second insert will overwrite the first document silently.
CMongoDB will automatically generate a new _id for the second document.
DThe second insert will fail with a duplicate key error.
Attempts:
2 left
💡 Hint
Remember that _id must be unique in a collection.
🧠 Conceptual
advanced
2:00remaining
Why does MongoDB use a 12-byte ObjectId for _id by default?
MongoDB's default _id is a 12-byte ObjectId. Which of the following best explains the reason for this design?
ATo store a simple incrementing integer for easy sorting
BTo embed creation time and ensure uniqueness across machines and processes
CTo save space by using a fixed 4-byte integer
DTo allow storing large binary data inside the _id field
Attempts:
2 left
💡 Hint
Think about what information is useful for a unique identifier in distributed systems.
📝 Syntax
advanced
2:00remaining
Which MongoDB insert command will NOT generate an _id automatically?
Given the following insert commands, which one will NOT cause MongoDB to generate an _id field automatically?
MongoDB
db.collection.insertOne({ name: "Alice", _id: "custom_id_123" })
Adb.collection.insertOne({ name: "Alice" })
Bdb.collection.insertOne({ name: "Alice", _id: ObjectId() })
Cdb.collection.insertOne({ name: "Alice", _id: "custom_id_123" })
Ddb.collection.insertOne({ name: "Alice", _id: null })
Attempts:
2 left
💡 Hint
When you provide an _id explicitly, MongoDB does not generate one.
🔧 Debug
expert
3:00remaining
Why does this MongoDB insert fail with a duplicate key error?
You run the following commands: 1. db.users.insertOne({ name: "Bob" }) 2. db.users.insertOne({ _id: ObjectId("507f1f77bcf86cd799439011"), name: "Carol" }) 3. db.users.insertOne({ _id: ObjectId("507f1f77bcf86cd799439011"), name: "Dave" }) The third insert fails with a duplicate key error. Why?
ABecause the _id value in the third insert is the same as the second insert's _id
BBecause the first insert generated an _id that conflicts with the third insert
CBecause ObjectId values cannot be used as _id fields
DBecause the collection does not allow inserting documents with names starting with 'D'
Attempts:
2 left
💡 Hint
Check the _id values used in the inserts carefully.