Recall & Review
beginner
What is the default type of the
_id field in MongoDB documents?The default
_id field in MongoDB is an ObjectId, a 12-byte unique identifier automatically generated for each document.Click to reveal answer
beginner
Can you assign a custom value to the
_id field in MongoDB?Yes, you can assign any unique value to the
_id field when inserting a document, such as a string, number, or ObjectId, as long as it is unique within the collection.Click to reveal answer
intermediate
Why might you want to use a custom
_id value instead of the default ObjectId?Using a custom
_id can help you use meaningful identifiers like usernames or codes, avoid extra lookups, or integrate with existing systems that have their own unique IDs.Click to reveal answer
beginner
What happens if you insert two documents with the same custom
_id value?MongoDB will reject the second insert with a duplicate key error because
_id must be unique in a collection.Click to reveal answer
beginner
How do you insert a document with a custom
_id in MongoDB using the shell?Use the insert command with the
_id field set to your custom value, for example:<br>{ _id: "user123", name: "Alice" }Click to reveal answer
What type of value can the
_id field have in MongoDB?✗ Incorrect
The
_id field can be any unique value, not just ObjectId.What happens if you insert a document without specifying
_id?✗ Incorrect
MongoDB automatically generates an ObjectId for
_id if you don't specify one.Why is the
_id field important in MongoDB?✗ Incorrect
_id uniquely identifies each document in a collection.If you want to use a username as the
_id, what must you ensure?✗ Incorrect
The
_id must be unique, so usernames used as _id must be unique.What error occurs if you insert a document with a duplicate
_id?✗ Incorrect
MongoDB throws a duplicate key error if
_id is not unique.Explain how and why you might use a custom
_id value in MongoDB.Think about identifiers you already use in real life, like usernames or codes.
You got /4 concepts.
Describe what happens if you try to insert two documents with the same
_id in MongoDB.Consider the role of <code>_id</code> as a unique identifier.
You got /3 concepts.