0% found this document useful (0 votes)
198 views

Spring Data MongoDB Cheatsheet

This document provides an overview of using Spring Data MongoDB with Spring Boot to configure a MongoDB database and perform CRUD operations. It outlines how to add dependencies, configure the database connection settings, define domain models annotated with MongoDB annotations, implement repositories extending Spring Data interfaces, and write queries using method names, the @Query annotation, and support for pagination and sorting.

Uploaded by

Robin Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
198 views

Spring Data MongoDB Cheatsheet

This document provides an overview of using Spring Data MongoDB with Spring Boot to configure a MongoDB database and perform CRUD operations. It outlines how to add dependencies, configure the database connection settings, define domain models annotated with MongoDB annotations, implement repositories extending Spring Data interfaces, and write queries using method names, the @Query annotation, and support for pagination and sorting.

Uploaded by

Robin Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Sold to

Spring Data MongoDB CheatSheet


robin.424242@gmail.com

Using Spring Boot

Dependency Configure Database Configuration

<dependency> Configure DB in application properties (


. or yaml) By default, all @Document andRepository are scanned, starting at the

<groupId>org.springframework.boot</groupId> location your main @Configuration


<artifactId>spring-boot-starter-data-mongodb</artifactId> spring.data.mongodb.uri=mongodb://localhost:27017/tutorial
</dependency> spring.data.mongodb.username=user @EnableMongoRepositories : Scans and sets up Repositories

spring.data.mongodb.password=myfancypassword
Example:

@EnableMongoRepositories("de.codeboje.example.repos")

Domain Model Repository

Create an interface and extend from Repository


@Document : defines a model @Document
@Id : primary key public class User { Example:

@Field : additional field config

@DBRef : reference model in another @Id public interface UserRepository extends PagingAndSortingRepository<User, Long>{
collection private Long id; }

private String username; Special Repositories

}
CrudRepository : adds basic CRUD methods

PagingAndSortingRepository : adds paging and sorting methods

Query

Defined on Repository methods

By Method Naming Using @Query Annotation Paging

findBy, findAllBy, followed by fieldnames joined MongoDB JSON query is passed in the parameter Pass a parameter of type Pageable and return Page
using AND, OR, etc

@Query("{ 'firstname' : ?0 }") Page<User> findAllByLastname(String lastname, Pageable page);


findAllByLastname(String lastname); List<User> findAllByLastname(String lastname);

findFirstByCreationDate Sorting

findAllByCityOrderByZipCode Pass a parameter of type Sort

List<User> findAllByLastname(String lastname, Sort sort);

codeboje.de

You might also like