Using the Mongo template component to access MongoDB

Using the repository interface provides a convenient way to create applications using standard CRUD operations, which are prebuilt around the fields contained in the model. If you need fine grained control over your data, an alternative is to use MongoTemplate, which is located in the package org.springframework.data.document.mongodb.

The simplest way to use MongoTemplate is to wire the MongoOperations component in your code, which is the superinterface of MongoTemplate. This interface replicates the methods that are already available in the Mongo driver, thus making the transition simpler between the driver and MongoTemplate. For example, you can execute methods such as find, findOne, findAndModify ...

Get MongoDB for Java Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.