In Spring Data MongoDB, @Document marks a Java class as a domain object that is stored in a MongoDB collection. Its collection attribute sets the collection name. When you leave it out, Spring Data uses the class name with the first letter in lowercase, so User maps to a collection called user.
The annotation is org.springframework.data.mongodb.core.mapping.Document. It is not strictly required for mapping, but you need it as soon as you want a custom collection name, collation or language setting.
@Document does@Document doesMongoDB stores BSON documents in collections. Spring Data MongoDB's mapping layer converts between your Java objects and those documents. @Document is a mapping annotation that gives the converter extra information about where an entity lives.
Spring Data MongoDB can actually map a plain class without @Document, because it only needs an identifier property to treat it as an entity. In practice most teams add it to every aggregate root, because it documents intent and allows configuration.
The collection attribute is an alias of value, so @Document("users") is equivalent. A String field named id or annotated with @Id maps to the _id field and is stored as an ObjectId when the string is a valid hex id.
The stored document looks like this.
The _class field is a type hint that Spring Data writes so it can instantiate the right class when reading polymorphic data.
| Attribute | Purpose |
|---|---|
collection or value | Collection name. Supports SpEL expressions |
language | Default language for text indexes |
collation | Collation for queries and index creation, for example a locale based string comparison |
expireAfter | Time to live for documents, expressed as a duration string |
Because the name accepts SpEL, you can resolve it at runtime, which is useful for multi tenant setups.
Spring Data derives the query from the method name and reads from the collection named by @Document.
@Id marks the identifier and maps to _id.@Field renames a property or sets its order in the document.@Indexed, @CompoundIndex and @TextIndexed declare indexes. Automatic index creation is off by default in Spring Data MongoDB 3.0 and later, so enable it with spring.data.mongodb.auto-index-creation=true or create indexes explicitly.@Transient excludes a property from persistence.@Version enables optimistic locking.@DBRef stores a reference to another document, although embedding is usually preferred in MongoDB.@TypeAlias replaces the fully qualified class name in _class with a stable alias, which survives refactoring.Spring Data Elasticsearch also has an annotation called @Document, in org.springframework.data.elasticsearch.annotations. It names an index instead of a collection. Check the import when the wrong one is picked by your IDE, because both compile and fail in confusing ways at runtime.
user, not users.@Indexed to create indexes without enabling auto index creation._class hints and then failing to read old data. Use @TypeAlias.jakarta.persistence.Entity and @Document on the same class.@Document mandatory in Spring Data MongoDB?No, a class with an @Id property can be mapped without it. It is needed for a custom collection name, collation, language or TTL settings.
The simple class name with a lowercase first letter. OrderItem becomes orderItem.
@Document on a nested object?You do not need to. Embedded objects are stored inside the parent document without their own annotation. Use @Document only for types stored in their own collection.
Yes, recent Spring Data versions support immutable types such as records through constructor binding.
@Document ties a Java class to a MongoDB collection. Set collection when the default decapitalised class name is not what you want, combine it with @Id, @Field and @Indexed, remember that indexes are not created automatically by default, and use @TypeAlias to keep stored type hints stable across refactoring.