Bird
Raised Fist0
MongoDBquery~20 mins

Custom _id values in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
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.

Practice

(1/5)
1.

What is the purpose of the _id field in a MongoDB document?

easy
A. It uniquely identifies each document in a collection.
B. It stores the creation date of the document.
C. It holds the user's login information.
D. It contains the document's size in bytes.

Solution

  1. Step 1: Understand the role of _id in MongoDB

    The _id field is a unique identifier for each document in a collection, ensuring no two documents share the same _id.
  2. Step 2: Compare with other options

    Other options describe unrelated fields or metadata, not the unique identifier role.
  3. Final Answer:

    It uniquely identifies each document in a collection. -> Option A
  4. Quick Check:

    _id = unique document ID [OK]
Hint: Remember: _id means unique ID for each document [OK]
Common Mistakes:
  • Thinking _id stores creation date
  • Confusing _id with user data fields
  • Assuming _id is optional
2.

Which of the following is the correct way to insert a document with a custom _id value in MongoDB?

db.users.insertOne({ _id: 123, name: "Alice" })
easy
A. db.users.insertOne({ id: 123, name: "Alice" })
B. db.users.insertOne({ _id: 123, name: "Alice" })
C. db.users.insertOne({ _id: "name", name: "Alice" })
D. db.users.insertOne({ _id: ObjectId(), name: "Alice" })

Solution

  1. Step 1: Identify correct _id field usage

    The _id field must be named exactly _id to set a custom ID. db.users.insertOne({ _id: 123, name: "Alice" }) uses _id: 123 correctly.
  2. Step 2: Check other options for errors

    db.users.insertOne({ id: 123, name: "Alice" }) uses id instead of _id. db.users.insertOne({ _id: "name", name: "Alice" }) uses a string "name" which is valid but less meaningful here. db.users.insertOne({ _id: ObjectId(), name: "Alice" }) uses default ObjectId, not custom.
  3. Final Answer:

    db.users.insertOne({ _id: 123, name: "Alice" }) -> Option B
  4. Quick Check:

    Custom _id needs exact field name [OK]
Hint: Use exact field name _id for custom IDs [OK]
Common Mistakes:
  • Using id instead of _id
  • Confusing ObjectId() with custom values
  • Using invalid types for _id
3.

Given the following documents inserted into a collection:

<pre>db.products.insertMany([ { _id: "p1", name: "Pen" }, { _id: "p2", name: "Pencil" }, { _id: "p3", name: "Eraser" } ]) What will db.products.find({ _id: "p2" }).toArray() return?

medium
A. [{ _id: "p2", name: "Pencil" }]
B. [{ _id: "p1", name: "Pen" }]
C. []
D. Error: Invalid query

Solution

  1. Step 1: Understand the query filter

    The query searches for a document with _id equal to "p2".
  2. Step 2: Match the document in the collection

    The document with _id: "p2" has the name "Pencil" and exists in the collection.
  3. Final Answer:

    [{ _id: "p2", name: "Pencil" }] -> Option A
  4. Quick Check:

    Query by custom _id returns matching document [OK]
Hint: Query by exact _id returns matching document [OK]
Common Mistakes:
  • Expecting multiple documents returned
  • Confusing _id with other fields
  • Assuming query returns error for string _id
4.

Consider this insertion attempt:

db.orders.insertOne({ _id: 101, item: "Book" })
db.orders.insertOne({ _id: 101, item: "Notebook" })

What error will occur and why?

medium
A. SyntaxError due to missing quotes around _id value.
B. TypeError because _id must be a string.
C. No error; both documents inserted successfully.
D. DuplicateKeyError because _id must be unique.

Solution

  1. Step 1: Check uniqueness requirement of _id

    The _id field must be unique in a collection. Both documents use _id: 101.
  2. Step 2: Identify the error caused by duplicate _id

    Inserting the second document with the same _id causes a DuplicateKeyError.
  3. Final Answer:

    DuplicateKeyError because _id must be unique. -> Option D
  4. Quick Check:

    Duplicate _id causes insertion error [OK]
Hint: No two documents can share the same _id [OK]
Common Mistakes:
  • Thinking _id can repeat
  • Confusing syntax error with duplicate key error
  • Assuming _id must be string only
5.

You want to store user profiles with custom _id values based on their email addresses to speed up lookups. Which approach is best?

// Option A
{ _id: ObjectId(), email: "user@example.com", name: "User" }

// Option B
{ _id: "user@example.com", name: "User" }

// Option C
{ email: "user@example.com", name: "User" }

// Option D
{ _id: UUID(), email: "user@example.com", name: "User" }
hard
A. Use default ObjectId and store email separately.
B. Do not use _id, just store email field.
C. Set _id to the email string for direct lookup.
D. Use UUID as _id and email separately.

Solution

  1. Step 1: Understand the goal of custom _id

    The goal is to speed up lookups by using email as the unique identifier.
  2. Step 2: Evaluate options for best fit

    Setting _id to the email string enables direct lookup using the email as _id, making queries fast. Using default ObjectId or UUID with separate email requires additional indexing. Omitting _id is invalid since it is mandatory.
  3. Final Answer:

    Set _id to the email string for direct lookup. -> Option C
  4. Quick Check:

    Custom _id as email speeds queries [OK]
Hint: Use email as _id for fast direct lookups [OK]
Common Mistakes:
  • Ignoring _id uniqueness requirement
  • Using default IDs when custom IDs help
  • Not indexing email for fast queries