liveBook · Manning

State persistence

As explained in MongoDB 8.0 in Action, Third Edition

Persistence is the durable storage of program state so that it survives beyond a single program execution.

State persistence stores program data so it remains available after a program stops. A customer or invoice object exists in memory during execution; persisting it lets a later run retrieve and work with its state. section 1.1

MongoDB stores data as documents, reducing the transformation needed to persist object-like structures. Documents can contain nested data and vary in their fields. That flexibility suits changing application data, while a relational database’s strict schema can provide stronger structural control for applications with precise transactional requirements. section 1.1 section 1.6

Listing 1.1 A MongoDB document with arrays and nested documents
{
  "_id": 1,
  "title": "MongoDB 8.0 in Action",
  "publisher": "Manning Publications",
  "status": "Available",
  "focusAreas": [
    "MongoDB Database System",
    "Atlas Platform"
  ],
  "publicationYear": 2025,
  "additionalDetails": {
    "embeddedDocument": {
      "description": "A comprehensive guide to mastering
MongoDB 8.0, including working with the Atlas
Platform and learning the latest features"
    }
  }
}

This document shows how an object-like record can keep an array and nested details together in persistent storage. section 1.1

Two practical ways to use persistent documents are:

  • Retrieve or change a stored record with MongoDB queries. For example, `db.books.find({ "title": "MongoDB 8.0 in Action" })` looks up a document by title; `updateOne` can change its publisher field. section 1.1
  • Persist stream-processing results by reading events with `$source`, aggregating them within a `$tumblingWindow`, and writing the results to an Atlas collection with `$merge`. section 15.6.6
let m = {
   $merge: {
      into: {
         connectionName: "mongodb-in-action-connection",
         db: "spiDB",
         coll: "spiColl"
      }
   }
}

The `$merge` stage names the Atlas connection, database, and collection that receive the processed results. section 15.6.6

Writing stream results to a collection preserves those results; it does not, by itself, establish how a stream processor’s internal state survives a failure. Choose what to store based on what the application must retrieve later. section 15.6.6 section 1.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines persistence, illustrates document storage, and walks through a streaming write pipeline; does not establish processor-state recovery behavior.
Examples
Uses a nested book document, find and update commands, and solar-device aggregates written with `$merge`.
What it assumes
Assumes familiarity with program objects while showing MongoDB document examples and the stages needed for a streaming write.
What you can do after
Apply it via MongoDB queries to retain and retrieve documents, or via Atlas Stream Processing to store aggregated results.
How it connects
Links persistence to document databases, flexible schemas, BSON, the MongoDB Query API, and Atlas Stream Processing.

Also explained in

Compare how 7 books explain state persistence →

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.