ReactiveMongoTemplate is the core class for non-blocking MongoDB access in Spring Data MongoDB. It runs queries, updates, aggregations and transactions on the MongoDB Reactive Streams driver and returns Mono and Flux results from Project Reactor. You use it when repository methods are not enough, for example for dynamic criteria, partial updates, aggregation pipelines or custom collection handling.
It is the reactive counterpart of MongoTemplate, and ReactiveMongoRepository is built on top of it.
ReactiveMongoTemplate is and when to choose itReactiveMongoTemplate is and when to choose itSpring Boot auto-configures a ReactiveMongoTemplate bean when spring-boot-starter-data-mongodb-reactive is on the classpath and a MongoDB connection is set. Nothing runs until something subscribes to the returned Mono or Flux, which in a WebFlux application the framework does when it writes the response.
| Need | Use |
|---|---|
| Basic CRUD and derived queries | ReactiveMongoRepository |
| Dynamic queries built at runtime | ReactiveMongoTemplate with Query and Criteria |
| Partial and atomic updates | ReactiveMongoTemplate.updateFirst, findAndModify |
| Aggregation pipelines | ReactiveMongoTemplate.aggregate |
| Change streams | ReactiveMongoTemplate.changeStream |
| Multi-document transactions | ReactiveMongoTransactionManager or inTransaction |
You can combine both: keep simple methods in a repository and add a custom fragment that uses the template for the rest.
Spring Boot 4 renames some property groups and starter modules, so check the property names for your Boot line if the connection does not apply.
The example uses a Java record as the document. Spring Data MongoDB supports records for reads and writes.
decreaseStock is atomic because the filter and the $inc are applied in one MongoDB operation. The stock >= amount condition prevents negative stock without a read-modify-write race. A returned count of 0 means no document matched.
aggregate returns a Flux of results, and the pipeline runs on the server.
MongoDB transactions need a replica set or sharded cluster. Use ReactiveMongoTransactionManager with @Transactional, or run a block through a TransactionalOperator.
A standalone MongoDB server rejects transactions, so use a replica set even in development, for example a single-node replica set in a container.
For capped collections, tail keeps a cursor open and emits new documents. For modern use, changeStream watches inserts, updates and deletes, and it needs a replica set.
block() inside a reactive pipeline, which can deadlock event-loop threads. Reactor raises an error when you block on a non-blocking thread.Mono that nobody subscribes to never touches the database.findAll().collectList() on large collections and holding everything in memory. Stream with Flux and apply backpressure or limits.@Indexed or @CompoundIndex, or manage them in migrations.ReactiveMongoTemplate and MongoTemplate?MongoTemplate blocks the calling thread and returns plain objects and lists. ReactiveMongoTemplate uses the reactive driver and returns Mono and Flux.
ReactiveMongoTemplate outside WebFlux?Yes. It only needs Reactor, so you can use it in any application that handles reactive types, or call block() from a plain thread in tests.
ReactiveMongoTemplate replace the repository?No. Both are available together, and repositories give you derived queries with less code.
Add skip and limit to the Query, or use query.with(Pageable), and run a separate count call when you need the total.
ReactiveMongoTemplate is Spring Data's low-level, non-blocking MongoDB API. It covers dynamic queries, atomic partial updates, aggregations, change streams and transactions, all returning Mono and Flux. Use repositories for simple access, and reach for the template when you need control, and keep the chain non-blocking from the controller to the database.