spring-data-jpa-mongodb-expressions

Use the MongoDB query language to query your relational database, typically from frontend.

APACHE-2.0 License

Stars
95

Spring Data JPA MongoDB Expressions

How it works:

  1. Customize JPA Repository base class:
    @SpringBootApplication
    @EnableJpaRepositories(repositoryBaseClass = ExpressionsRepositoryImpl.class)
    public class Application { … }
    
  2. Change your repository to extend ExpressionsRepository:
    @Repository
    public interface EmployeeRepository extends ExpressionsRepository<Employee, Long> {
    }
    
  3. Build the controller/service:
    @PostMapping("/search")
    public ResponseEntity<Page<EmployeeDto>> search(@RequestBody Expressions expressions, Pageable pageable) {
    
        return ok().body(
                    employeeRepository.findAll(expressions, pageable).map(employeeMapper::toDto)
            );
    }
    
  4. Send Mongodb query in JSON from frontend:
    {
      "$or": [
        {"lastName": "ibrahim"},
        {
          "$and": [
            {"firstName": "mostafa"},
            {"birthDate": {"$gt": "1990-01-01"}}
          ]
        }
      ]
    }
    

Learn more

For a quick start see this Medium post or dev.to post or see this demo example on Github.

See documentation website for details about how to get started.

Install:

For spring-boot 3.x:

<dependency>
  <groupId>com.github.mhewedy</groupId>
  <artifactId>spring-data-jpa-mongodb-expressions</artifactId>
  <version>0.1.3</version>
</dependency>

For spring-boot 2.x:

<dependency>
  <groupId>com.github.mhewedy</groupId>
  <artifactId>spring-data-jpa-mongodb-expressions</artifactId>
  <version>0.0.8</version>
</dependency>

🎖 Special Thanks

Special thanks to Rashad Saif and Hamada Elnoby for helping in the design, inspring with ideas, and for doing code review.

In the News

This repo has been mentioned in spring.io weekly news.

Package Rankings
Top 6.73% on Proxy.golang.org
Top 31.57% on Repo1.maven.org
Badges
Extracted from project README
Java CI with Maven codecov javadoc Join the chat at https://gitter.im/spring-data-jpa-mongodb-expressions/community Tweet
Related Projects