How to use SpringData MongoRepository to interact with MongoDB
Tags: mongodb, springdata, springboot
Published: 2021-04-09 09:37:31

https://grokonez.com/spring-framework/spring-data/how-to-use-springdata-mongorepository-to-interact-with-mongdb-springboot-application

How to use SpringData MongoRepository to interact with MongoDB

How to use SpringData MongoRepository to interact with MongoDB

In the past <a href="https://grokonez.com/spring-framework/spring-boot/spring-data-mongooperations-accessing-mongo-database">post</a>, we had learned how to work with <strong>MongoDB</strong> by <strong>MongoOperations</strong> which supports a set of standard APIs to manipulate data. So in the tutorial, <strong>JavaSampleApproach</strong> will show a powerful tool <strong>SpringData MongoRepository</strong> with flexible and more complex APIs to interact with <strong>MongoDB</strong>.

Related articles:
- <a href="https://grokonez.com/spring-framework/spring-boot/spring-data-mongooperations-accessing-mongo-database">Spring MongoOperations to access MongoDB</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/build-springboot-mongodb-restfulapi">How to build SpringBoot MongoDb RestfulApi</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/springdata-mongodb-gridfstemplate-save-retrieve-delete-binary-files-image-text-files">SpringData MongoDB GridFsTemplate to save, retrieve, delete binary files (Image, Text files)</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/angular-4-spring-boot-mongodb-crud-example">Angular 4 + Spring Boot + MongoDB CRUD example</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/spring-boot-angular-6-example-spring-data-rest-mongodb-crud-example">Spring Boot + Angular 6 example | Spring Data + REST + MongoDb CRUD example</a>

<!--more-->
<iframe width="853" height="480" src="https://www.youtube.com/embed/fx3Gwsgv0V8" frameborder="0" allowfullscreen></iframe>
<h2>I. Technologies</h2>
– Java: 1.8
– Maven: 3.3.9
– Spring Tool Suite: Version 3.8.4.RELEASE
– Spring Boot: 1.5.4.RELEASE
- MongoDB: v3.4.1
<h2>I. MongoRepository</h2>
<strong>MongoRepository</strong> is implemented with lots of APIs to store and retrieve data. We can use <strong>MongoRepository</strong> with full generic CRUD operations of <strong>CrudRepository</strong> interface, and additional methods of <strong>PagingAndSortingRepository</strong> interface for pagination and sorting.
<strong>MongoRepository</strong> also extends <strong>QueryByExampleExecutor</strong> so it allows execution of <strong>Query by Example</strong>.

<pre><code class="language-java">
public interface MongoRepository<T, ID extends Serializable>
		extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
...


public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {
</code></pre>

-> With a strong set of APIs, We should start Spring <strong>MongoRepository</strong> to manipulate data from <strong>MongoDB</strong>.

<img src="https://grokonez.com/wp-content/uploads/2017/06/SpringData-Mongodb-architectures-c.png" alt="SpringData Mongodb - architectures c" width="388" height="370" class="alignnone size-full wp-image-6223" />
<h2>II. Practice</h2>

More at:

https://grokonez.com/spring-framework/spring-data/how-to-use-springdata-mongorepository-to-interact-with-mongdb-springboot-application