Using ReactiveMongoTemplate

Even though the ReactiveMongoTemplate is used as a building block of a reactive repository, the class itself is very versatile. Sometimes it allows working with the database more efficiently than the higher-level repository does.

For example, let's implement a simple service that uses ReactiveMongoTemplate to find books by title using a regular expression. The implementation may look as follows:

public class RxMongoTemplateQueryService {
   private final ReactiveMongoOperations mongoOperations;            // (1)
   // Constructor...

   public Flux<Book> findBooksByTitle(String titleRegExp) {          // (2)
      Query query = Query.query(new Criteria("title")                // (3)
         .regex(titleRegExp)))
         .limit(100);
      return mongoOperations
         .find(query, Book.class, "book");                           // (4)
   }
}

Let's describe the main points of the RxMongoTemplateQueryService class:

  1. We have to reference the instance of the ReactiveMongoOperations interface. ReactiveMongoTemplate implements that interface and is present in the Spring context when the MongoDB data source is configured.
  2. The service defines the findBooksByTitle method and consumes regular expressions as search criteria and returns a Flux with results.
  1. The MongoDB connector's Query and Criteria classes are used to build an actual query with the regular expression. Also, we limit the number of results to 100 by applying the Query.limit method.
  2. Here, we are asking mongoOperations to execute the previously built query. Query results should be mapped to entities of the Book class. Also, we have to tell what collection we are using for the query. In the preceding example, we query a collection called book.
Note that we may achieve the same behavior (except for the query limit) with an ordinary reactive repository by providing the following method signature, which follows the naming convention:
Flux<Book> findManyByTitleRegex(String regex);

Under the hood, ReactiveMongoTemplate uses the ReactiveMongoDatabaseFactory interface to get an instance of reactive MongoDB connections. Also, it uses an instance of the MongoConverter interface to convert entities to documents and vice versa. MongoConverter is also used in a synchronous MongoTemplate. Let's look at how ReactiveMongoTemplate implements its contract. For example, the find(Query query,...) method maps the org.springframework.data.mongodb.core.query.Query instance to an instance of the org.bson.Document class, with which the MongoDB client is capable of working. Then ReactiveMongoTemplate invokes the database client with the converted query. The com.mongodb.reactivestreams.client.MongoClient class gives an entry point to the reactive MongoDB driver. It is Reactive Streams-compliant and returns data through reactive publishers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.219.130