liveBook · Manning

Document

As explained in Spring in Action, Sixth Edition

A document is a self-contained record in a document database that stores data as structured fields and values.

A document is a self-contained MongoDB record represented by a domain class. Spring Data MongoDB uses @Document to mark the class for persistence and @Id to identify the stored record. Unannotated properties use matching field names unless @Field or @Transient changes that behavior. section 4.2.2

Map a domain type to a MongoDB document by applying the annotations that fit its persistence needs:

  • @Document declares a persistable document and derives the collection name from the class unless collection is specified. section 4.2.2
  • @Id marks a Serializable property as the document identifier. section 4.2.2
  • @Field overrides a persisted field name, while @Transient excludes a property. section 4.2.2
  • Keep related classes such as Taco inside a TacoOrder document when they belong to the aggregate rather than being stored independently. section 13.2.1

Ingredient and TacoOrder

An Ingredient class can use @Document and @Id so instances are stored in the ingredient collection by default. A TacoOrder class can also be a document while holding a list of Taco objects; Taco then belongs to the order instead of requiring its own stored document. section 4.2.2 section 13.2.1

package tacos;
 
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
 
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 
@Data
@Document
@AllArgsConstructor
@NoArgsConstructor(access=AccessLevel.PRIVATE, force=true)
public class Ingredient {
 
  @Id
  private String id;
  private String name;
  private Type type;
 
  public enum Type {
    WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
  }
 
}

Use the complete Ingredient listing to see class-level @Document mapping, an @Id property, ordinary persisted fields, and Lombok-generated support. section 4.2.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains core mapping annotations and collection naming, but does not descend into MongoDB storage internals.
Examples
Uses Ingredient, Taco, and TacoOrder classes, including embedded tacos and annotated identifiers.
What it assumes
Assumes domain classes and MongoDB, while teaching the core annotations needed to map those classes.
What you can do after
Apply it via Spring Data MongoDB by annotating domain classes and choosing document identifiers, fields, and collection names.
How it connects
Connects documents with MongoDB collections, aggregate roots, domain-driven design, and reactive or nonreactive repositories.

Also explained in

Compare how 8 books explain Document →

Related concepts

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