liveBook · Manning

Document

As explained in MongoDB 8.0 in Action, Third Edition

A document is a JSON-like data structure containing named fields and their values, potentially including arrays and nested documents.

A document is a MongoDB record made of named fields and values. Documents live in collections, which live in databases. MongoDB stores them as BSON, an extension of JSON with additional data types. A field can hold a string, number, date, array, or nested document. section 2.6 section 2.6.4

Documents can keep related application data together rather than forcing it into separate tables. Their fields and types can differ within one collection, so a structure can evolve as requirements change. That flexibility still calls for deliberate schema design: consider relationships, query patterns, and likely changes when deciding what belongs in a document. section 1.1 section 5.0

Inspect a document

In mongosh, select the sample database with `use sample_training`, then run `db.grades.findOne()`. The returned academic record has an `_id`, student and class identifiers, and a `scores` array whose entries contain assessment types and scores. If no top-level `_id` is supplied on insertion, MongoDB adds one using an ObjectId. section 2.6.4

Listing 2.7 Displaying a single MongoDB document using findOne()
[primary] sample_training> use sample_training
already on db sample_training

[primary] sample_training> db.grades.findOne()

{
  _id: ObjectId('56d5f7eb604eb380b0d8d8ce'),
  student_id: 0,
  scores: [
    { type: 'exam', score: 78.40446309504266 },
    { type: 'quiz', score: 73.36224783231339 },
    { type: 'homework', score: 46.980982486720535 },
    { type: 'homework', score: 76.67556138656222 }
  ],
  class_id: 339
}

[primary] sample_training>

The shell result shows field–value pairs and an array of embedded assessment documents in one record. section 2.6.4

A BSON document cannot exceed 16 MB; the limit prevents one record from consuming excessive memory or transmission bandwidth. Keep that boundary in mind when gathering related data into a document. section 2.6.4

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines BSON fields, arrays, nesting, identifiers, and the 16 MB limit, then demonstrates inspection with findOne().
Examples
Shows a grades record with embedded scores and a book record with arrays and nested details.
What it assumes
Introduces the storage hierarchy and document inspection from basic application-record concepts without requiring a predefined schema.
What you can do after
Apply it via mongosh to inspect a document with findOne() and plan fields around application queries.
How it connects
Connects documents to collections, BSON, ObjectId, flexible schemas, and query-driven data modeling.

Also explained in

Compare how 8 books explain Document →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.