Just as the traditional repositories are based on traditional template implementations, the reactive ones are built on top of a reactive template. Most of the operations available in the blocking template API have a counterpart in the reactive template. We’re going to port more features of the blocking world into the reactive template APIs, but some of the operations are simply not available through reactive drivers (yet) or just do not make sense in a reactive world.

Here’s is an excerpt from ReactiveMongoOperations from Spring Data MongoDB. It is implemented by ReactiveMongoTemplate and uses Project Reactor's reactive types such as Mono and Flux to wrap responses. Some methods also accept reactive types to stream data into your data store.

public interface ReactiveMongoOperations { // … /** * Map the results of an ad-hoc query on the specified collection to a * single instance of an object of the specified type. */ <T> Mono<T> findOne(Query query, Class<T> entityClass); /** * Map the results of an ad-hoc query on the collection for the entity * class to a List of the specified type. */ <T> Flux<T> find(Query query, Class<T> entityClass); /** * Insert the object into the specified collection. */ <T> Mono<T> insert(T objectToSave, String collectionName); /** * Insert the object into the collection for the entity type of the object * to save. */ <T> Mono<T> insert(Mono<? extends T> objectToSave); // … }

Note, that all methods follow the reactive execution model, not executing any operations incorporating any I/O upon invocation but only on subscription to the returned value.

Let’s insert some data via the template:

Flux<Person> flux = Flux.just(new Person("Walter", "White"), new Person("Skyler", "White"), new Person("Saul", "Goodman"), new Person("Jesse", "Pinkman")); template.insertAll(flux).subscribe();

Some methods — like insertAll(…) — accept reactive types to stream incoming data asynchronously into your MongoDB database, e.g. coming from a Flux that you receive in a Spring Web Reactive controller that will asynchronously map a JSON array via Jackson:

@PostMapping("/people") Flux<People> namesByLastname(@RequestBody Flux<Person> people) { return template.insertAll(people); }