Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.
Guide · Web DevelopmentFree

Spring Data MongoDB Documentation

The reference for Spring Data MongoDB: mapping classes with @Document, repositories and derived query methods, MongoTemplate, the aggregation API, change streams, transactions and reactive support. The standard way to use MongoDB in Spring Boot, and this reference explains what the annotations actually do.

GitHub stars

1.7k (spring-data-mongodb)

GitHub · Sept 2026

Reference version

5.1.1 (stable)

Spring docs · Sept 2026

Requires

JDK 17+, Spring Framework 7

Spring docs · Sept 2026

Tested MongoDB versions

6.x to 8.x

Spring docs · Sept 2026

Format

Guide

Topic

Web Development

Provider

Spring

Time needed

A week alongside a Spring Boot project

Level

Intermediate

Access

Free

What Spring Data MongoDB is

Spring Data MongoDB is the Spring project that connects Spring applications to MongoDB. It maps your Java classes to documents, generates repository implementations from interface method names, and offers MongoTemplate for everything a repository cannot express. In Spring Boot it is what the MongoDB starter brings in. The reference covers the 5.1.x line, and states that the 5.x binaries "require JDK level 17 and above and Spring Framework 7.0.9 and above". Its compatibility table lists MongoDB 6.x to 8.x as tested server versions.

What the reference covers

  • MongoDB support and the Template API: configuration, index and collection management, saving, updating and removing documents, querying with Query and Criteria, counting and aggregation framework support.
  • Object mapping: @Document, @Id, @Field, @Indexed, custom conversions and JSON Schema generation.
  • Repositories and query methods: derived queries such as findByStatusOrderByCreatedAtDesc, @Query annotations, paging and sorting.
  • Change streams, sessions and transactions, tailable cursors, GridFS, auditing and lifecycle events.
  • Reactive support with Project Reactor, Kotlin support including coroutines, sharding, encryption, vector search and observability.

Is it worth reading, or is Spring Boot magic enough?

Spring Boot gets a repository working in minutes, which is exactly why this reference matters. The defaults decide real behaviour: whether indexes declared with @Indexed are created automatically (in recent versions they are not by default), how a BigDecimal is stored, how a derived query name becomes a filter, and what save does when the document already exists. Reading the mapping and repository chapters once saves hours of guessing.

What to read first

  1. Getting started and configuration, including the connection string property.
  2. Object mapping, so your domain classes produce the documents you expect. Check the result in Compass or mongosh.
  3. Repositories and query methods, for the everyday cases.
  4. The Template API and Criteria, for anything conditional or dynamic.
  5. Aggregation framework support, then transactions and change streams when you need them.

Prerequisites

Java 17 or later, working knowledge of Spring Boot and dependency injection, and basic MongoDB queries. Knowing the underlying Java driver helps when you debug connection or mapping errors, because Spring passes them through.

Common mistakes

  • Designing documents like JPA entities, with many small collections joined by references, instead of embedding what is read together.
  • Relying on long derived query names for complex filters rather than @Query or MongoTemplate.
  • Assuming indexes exist because an annotation is present.
  • Using transactions everywhere when a single-document update would keep the data consistent.

Common questions

Repositories or MongoTemplate? Both. Repositories for simple CRUD, the template for dynamic queries, bulk updates and aggregation.

Is it free? Yes, it is open source under Apache 2.0.

What can I do after it? Build Spring Boot services on MongoDB with mapped domain classes, repositories, custom queries, aggregation and transactions.

Best for: Java developers using Spring Boot who want MongoDB to behave predictably.

databasebackendjavaopen source

Ready to start?

Opens on Spring in a new tab.

Open resource
Link broken or out of date?

Report a problem

With "Spring Data MongoDB Documentation". Only Tauseef reads this.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.