Querying with MongoOperations

So far, we have delved into the repository solution using both query by property and Query by Example. There is another angle we can use, MongoTemplate.

MongoTemplate mimics the Spring Framework's JdbcTemplate, the first data access mechanism implemented by Spring. JdbcTemplate allows us to focus on writing queries while delegating connection management and error handling to the framework.

MongoTemplate brings the same power to bear on crafting MongoDB operations. It's very powerful, but there is a critical tradeoff. All code written using MongoTemplate is MongoDB-specific. Porting solutions to another data store is very difficult. Hence, it's not recommended as the first solution, but as a tool to keep in our back pocket for critical operations that require highly tuned MongoDB statements.

To perform reactive MongoTemplate operations, there is a corresponding ReactiveMongoTemplate that supports Reactor types. The recommended way to interact with ReactiveMongoTemplate is through its interface, ReactiveMongoOperations.

The tool that actually conducts MongoDB repository operations under the hood is, in fact, a MongoTemplate (or a ReactiveMongoTemplate depending on the nature of the repository).

Additionally, Spring Boot will automatically scan the classpath, and if it spots Spring Data MongoDB 2.0 on the classpath along with MongoDB itself, it will create a ReactiveMongoTemplate. We can simply request a copy autowired into our class, whether by constructor injection or field injection, as follows:

    @Autowired 
    ReactiveMongoOperations operations; 

@Autowired in the last code snippet indicates this field will be injected when the class is loaded, and we'll get a copy of the bean that implements ReactiveMongoOperations.

For test cases, field injection is fine. But for actual running components, the Spring team recommends constructor injection, as will be shown throughout this book. For more details about the benefits of constructor injection, read Spring Data lead Oliver Gierke's blog post at http://olivergierke.de/2013/11/why-field-injection-is-evil/.

Using ReactiveMongoOperations along with Query byExample, we can see the previous query rewritten as follows:

    Employee e = new Employee(); 
    e.setFirstName("Bilbo"); 
    Example<Employee> example = Example.of(e); 
 
    Mono<Employee> singleEmployee = operations.findOne( 
      new Query(byExample(example)), Employee.class); 

We can tear apart this latest wrinkle in MongoDB querying as follows:

  • The declaration of the probe and its example is the same as shown earlier
  • To create a query for one entry, we use findOne from ReactiveMongoOperations
  • For the first parameter, we create a new Query, and use the byExample static helper to feed it the example
  • For the second parameter, we tell it to return an Employee

Because this is ReactiveMongoOperations, the value is returned wrapped inside a Mono.

A similar tune-up can be made to fetch multiple entries with custom criteria, as follows:

    Employee e = new Employee(); 
    e.setLastName("baggins"); // Lowercase lastName 
 
    ExampleMatcher matcher = ExampleMatcher.matching() 
     .withIgnoreCase() 
     .withMatcher("lastName", startsWith()) 
     .withIncludeNullValues(); 
 
    Example<Employee> example = Example.of(e, matcher); 
 
    Flux<Employee> multipleEmployees = operations.find( 
      new Query(byExample(example)), Employee.class); 

Now let's check out the details of this preceding query:

  • The example is the same as the previous findAll query
  • This time we use find, which accepts the same parameters as findOne, but returns a Flux

ReactiveMongoOperations and its Query input opens up a world of powerful operations, like this:

    reactiveMongoOperations 
     .findOne( 
       query( 
         where("firstName").is("Frodo")), Employee.class)

Beyond that, there is support for updating documents, finding-then-updating, and upserting, all supporting the rich, native MongoDB operators through a fluent API.

Delving into more MongoDB operations is beyond the scope of this book, but it's within your grasp should the need arise.

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

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