0
0
MongoDBquery~5 mins

Custom _id values in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AOnly ObjectId
BAny unique value
COnly strings
DOnly numbers
What happens if you insert a document without specifying _id?
AMongoDB automatically creates an ObjectId for <code>_id</code>
BMongoDB rejects the insert
CThe document has no <code>_id</code>
DYou must specify <code>_id</code> manually
Why is the _id field important in MongoDB?
AIt stores the document's creation date
BIt is optional and has no special role
CIt uniquely identifies each document
DIt stores the document's size
If you want to use a username as the _id, what must you ensure?
AUsernames are unique in the collection
BUsernames are always numbers
CUsernames are ObjectIds
DUsernames are optional
What error occurs if you insert a document with a duplicate _id?
ASyntax error
BNo error, it overwrites
CConnection error
DDuplicate key error
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.