Spring Data MongoDB provides a repository-oriented way for Java applications to access MongoDB document databases. It follows the Spring Data model of repositories, object-mapping abstractions, annotations, derived queries, Spring integration, and Spring Boot configuration. section 17.0 section 17.2
A typical application can use Spring Data MongoDB to:
- Configure a Spring Boot project with the MongoDB starter and application properties. section 17.2
- Create MongoRepository interfaces with derived queries, result limits, sorting, paging, and streaming. section 17.7
- Use @Query, Query by Example, or MongoTemplate when repository methods are insufficient. section 17.7
Mapping a User document
A User class can be annotated with @Document and @Id, index username with @Indexed, declare compound indexes with @CompoundIndexes, exclude password with @Transient, and mark a retrieval constructor with @PersistenceConstructor. Lombok annotations can generate constructors, getters, and setters.

