Skip to main content

What is a primary key in MongoDB?

Entry MongoDB
Quick Answer Every MongoDB document has an _id field that serves as the primary key - it must be unique within the collection. By default, MongoDB auto-generates an ObjectId for _id. You can provide your own _id value (string, int, etc.) but it must be unique. The _id field is always indexed automatically.

Answer

Every document has a unique _id field, which acts as the primary key. MongoDB generates an ObjectId if not provided.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice