The @Document annotation marks a Java class as a persistent domain object in Spring Data MongoDB. It tells the mapping layer to store instances of that class as documents in a MongoDB collection, and it lets you choose the collection name, the collation and, in current releases, a language for text indexes.
The annotation is optional for simple cases, because Spring Data MongoDB can map plain classes by convention. You still add it in practice because it makes the mapping explicit, controls the collection name, and is what the classpath scanner looks for when it builds the mapping context.
@Document lives in org.springframework.data.mongodb.core.mapping. When the MongoMappingContext scans your entity classes, it builds metadata for each one: the collection it belongs to, which property is the _id, how fields are named and how values are converted to BSON.
Without @Document, a class passed to MongoTemplate or a repository is still mapped, and the collection name is derived from the simple class name with the first letter lower-cased (UserProfile becomes userProfile). With the annotation you override that default.
| Aspect | Without @Document | With @Document |
|---|---|---|
| Collection name | Derived from the class name | Explicit via collection or value |
| Entity scanning | Not picked up by @Document scanning | Found as a persistent entity |
| Collation | Server default | Set per collection with collation |
| Intent | Implicit | Explicit and self-documenting |
The collection attribute (and its alias value) sets the target collection explicitly. This is the most common reason to use the annotation.
Every User you save lands in the users collection. The id property, marked with @Id, maps to MongoDB's _id field. When it is a String and you leave it null, MongoDB generates an ObjectId and Spring converts it to a string when the entity is read back.
Collection names can also be computed with a SpEL expression, which is useful for tenant-specific collections:
The expression is evaluated against the application context, so tenantContext here must be a Spring bean you define yourself.
Spring Data MongoDB supports constructor-based object creation, so Java records work well as documents on Java 17 and later.
Spring uses the canonical constructor to create instances, so no setters are required. @Field renames a property in the stored document, so price is saved as unit_price. For an immutable type, saving an entity that has no id returns a new instance with the generated id populated, so always use the returned object.
@Document rarely stands alone. These annotations complete the mapping:
@Id marks the identifier property (a property named id or _id is also detected by convention).@Field sets the stored field name and, optionally, its order.@Indexed, @CompoundIndex and @TextIndexed declare indexes.@Transient excludes a property from persistence.@DBRef references another document, though embedding or storing an id is usually simpler and faster.@Version enables optimistic locking.Automatic index creation from annotations is disabled by default in Spring Data MongoDB 3.x and later. Enable it with spring.data.mongodb.auto-index-creation=true in Spring Boot, or create indexes through MongoTemplate.indexOps(...) or a migration tool, which is the safer choice in production.
Once the class is annotated, a repository interface gives you CRUD operations and derived queries.
The repository reads the collection name from the @Document metadata of User, so you never repeat the string in queries. Add the spring-boot-starter-data-mongodb dependency and set spring.data.mongodb.uri to connect.
org.springframework.data.mongodb.core.mapping.Document, not a Lucene, Elasticsearch or other Document type.collection value prevents this.null.@DBRef for joins. References are resolved by extra queries, so prefer embedding or $lookup for read-heavy paths.No. Classes without it are still mapped, with the collection named after the class. The annotation is recommended because it makes the collection explicit and keeps entity scanning predictable.
They are aliases for the same attribute. @Document("users") and @Document(collection = "users") are equivalent.
MongoDB creates a collection on the first write, so saving the first entity creates it. To create it earlier, with options such as validation or time series, use MongoTemplate.createCollection.
Yes. Records work as immutable documents on Java 17 and later because Spring Data uses the canonical constructor to build them.
@Document maps a class to a MongoDB collection, sets the collection name and collation, and makes the mapping explicit. Combine it with @Id, @Field and index annotations, set the collection name deliberately so refactoring cannot orphan data, and create production indexes through a controlled process rather than relying on automatic creation.