Skip to main content

What is a document in MongoDB?

A document in MongoDB is the basic unit of data storage, analogous to a row in SQL, but as a BSON object resembling JSON.

Key characteristics

  1. A "key-value" structure A document is a set of fields:
json
{ "name": "Alex", "age": 25, "skills": ["JS", "MongoDB"] }
  1. Flexibility Different documents in the same collection can have different sets of fields. They don't have to share the same "schema".
  2. Nesting Documents can hold nested objects and arrays, letting you store complex structures right inside a single record.
  3. A unique _id Every document has an _id field used as its primary key (created automatically if not set).

The essence of it: a document is a self-contained data record, stored in BSON format, supporting nesting and a flexible structure.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.