The @Document annotation in Spring Data MongoDB marks a Java class as a domain object that maps to a MongoDB collection, and it lets you set the collection name and a few collection-level options. Each instance of the class is stored as one document in that collection. Without a custom name, Spring Data uses the simple class name with the first letter in lower case, so User maps to user and OrderItem maps to orderItem.
The annotation lives in org.springframework.data.mongodb.core.mapping. It works together with @Id, @Field, @Indexed and the other mapping annotations, and it is the first thing to learn when you model data with Spring Boot and MongoDB.
@Document is a class-level annotation that tells the mapping layer: this type is persisted in MongoDB, and here is where. Spring Data scans the application package for annotated classes at startup and builds mapping metadata for each one. That metadata is used by MongoTemplate, by repositories such as MongoRepository, and by the converter that turns objects into BSON and back.
In practice the annotation gives you three things:
Be precise about the third point: a class without @Document can still be saved through MongoTemplate, because the mapping context creates metadata on demand. The annotation is still the convention, because it makes the collection explicit and keeps the intent visible in the code.
The smallest useful entity has @Document and an @Id field. The identifier is stored as the _id field of the document. A String id that looks like an ObjectId is converted to a real ObjectId when it is written, and MongoDB generates a value if you leave it null.
This class is stored in a collection named user. A saved instance looks like this in the shell:
The _class field is a type hint that Spring Data adds so it can read polymorphic data back into the right subclass. You can customise or remove it with @TypeAlias or by configuring the MappingMongoConverter.
Use the collection attribute, or its alias value, when the collection name should not follow the class name. This is common when you inherit an existing database or follow a naming convention such as plural lower-case names.
The collection name also accepts SpEL expressions, which lets you pick the name at runtime, for example per tenant:
Besides the name, the annotation supports a small set of options.
| Attribute | Purpose |
|---|---|
collection / value | Name of the MongoDB collection. |
language | Default language for text indexes on this collection, for example "english". |
collation | Default collation for queries, for example locale and strength rules for string comparison. |
A collation example that makes string comparisons case-insensitive:
@Document only describes the collection. Field-level behaviour comes from other annotations in the same class.
Here @Field("full_name") stores the Java property name under the key full_name, and @Indexed(unique = true) declares a unique index on email.
Since Spring Data MongoDB 3.0, automatic index creation is off by default. Declaring @Indexed is not enough on its own; turn it on explicitly in Spring Boot:
For production systems many teams skip this setting and manage indexes in migrations, so that an application restart never triggers a large index build.
With the class mapped, a repository gives you CRUD and derived queries with no extra configuration:
Spring Data resolves the customers collection from the @Document annotation on Customer. On current Spring Boot versions you can also model entities as Java records, as long as the record has a suitable constructor; Spring Data populates the document through that constructor.
No. Spring Data can map a class without it, using the default collection name. You should still add it, because it documents the collection name and keeps entity classes easy to find.
The simple class name with the first character in lower case, not the whole name in lower case. UserProfile becomes userProfile.
@Entity is a JPA annotation for relational tables. @Document is the Spring Data MongoDB equivalent for collections. Do not mix them on the same class unless you deliberately use both stores.
Automatic index creation has been disabled by default since Spring Data MongoDB 3.0. Set spring.data.mongodb.auto-index-creation=true or create the index yourself.
@Document maps a Java class to a MongoDB collection and lets you name that collection, with SpEL support, and set a default language and collation. Pair it with @Id for the _id field, @Field for key names and @Indexed for indexes, and remember that the default collection name is the class name with a lower-case first letter and that automatic index creation must be enabled explicitly.