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







