1. What field serves as the primary key in MongoDB documents?
The underscore id field is the primary key in MongoDB. It must be unique within a collection and is automatically generated if not provided during document insertion. MongoDB generates a unique ObjectId value for underscore id, which includes a timestamp, machine identifier, process ID, and a counter. This ensures uniqueness across distributed systems without coordination between servers.
// Auto-generated _id
db.users.insertOne({ name: "John" })
// Result: { _id: ObjectId("507f1f77bcf86cd799439011"), name: "John" }
// Custom _id
db.users.insertOne({ _id: 100, name: "Alice" })