0
0
MongoDBquery~20 mins

Custom _id values in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom _id Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What will be the _id of the inserted document?
Consider the following MongoDB insert operation where a custom _id is provided:

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" })
AAn ObjectId generated automatically
Bnull
C"user123"
DA numeric value starting from 1
Attempts:
2 left
💡 Hint
If you provide _id explicitly, MongoDB uses that value instead of generating one.
query_result
intermediate
2:00remaining
What happens if you insert two documents with the same custom _id?
Given the following two insert operations:

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" })
AThe second insert will fail with a duplicate key error
BThe second document will overwrite the first one
CBoth documents will be inserted successfully
DMongoDB will automatically change the _id of the second document
Attempts:
2 left
💡 Hint
The _id field must be unique in a collection.
📝 Syntax
advanced
2: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?
Adb.orders.insertOne({ _id: ObjectId, item: "Book" })
Bdb.orders.insertOne({ _id: ObjectId("507f1f77bcf86cd799439011"), item: "Book" })
Cdb.orders.insertOne({ _id: new ObjectId(), item: "Book" })
Ddb.orders.insertOne({ _id: "ObjectId(\"507f1f77bcf86cd799439011\")", item: "Book" })
Attempts:
2 left
💡 Hint
ObjectId needs to be called as a function with a string argument.
optimization
advanced
2: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?
AYou can use meaningful keys that improve query readability and indexing
BMongoDB will automatically optimize queries better with custom _id
CCustom _id values reduce storage size compared to ObjectIds
DCustom _id values allow multiple documents with the same _id
Attempts:
2 left
💡 Hint
Think about how meaningful keys can help when searching or indexing.
🧠 Conceptual
expert
2: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?
AIt will cause duplicate key errors automatically
BMongoDB does not support integer _id values
CSequential integers cause documents to be stored out of order
DIt can cause index hotspots leading to slower write performance
Attempts:
2 left
💡 Hint
Think about how indexes handle inserts with increasing keys.