juniorMongoDB
What is the purpose of the _id field?
Updated May 1, 2026
Short answer
The _id field is a unique identifier for every document in a collection.
Deep explanation
If not provided, MongoDB automatically generates a 12-byte ObjectId. It serves as the primary key and is automatically indexed.
Real-world example
Ensuring uniqueness when inserting customer records.
Common mistakes
- Trying to update the _id field (it is immutable).
Follow-up questions
- What parts make up an ObjectId?