Skip to content

tuana9a/spring-mongo-query-resolver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-mongo-query-resolver

This library's name has changed

  • release 1.x merij
  • release 2.x spring-data-mongodb
  • release >= 3.x spring-mongo-query-resolver

Breaking changes

  • release <= 4.x: java 8
  • release >= 4.x: java 17

What is this repo

Resolve http query string to mongo criteria and mongo sort.

Query operation support

  • $eq
  • $lt
  • $gt
  • $lte
  • $gte
  • $in
  • $ne
  • $regex

Sort operation support

  • asc
  • desc

How to use

Add dependency into your pom.xml

pom.xml

<dependencies>
    <dependency>
        <groupId>com.tuana9a</groupId>
        <artifactId>spring-mongo-query-resolver</artifactId>
        <version>4.0.0</version>
    </dependency>
<dependencies>

Resolve criteria query from string

// import com.tuana9a.spring.mongo.qrisolver.resolvers.CriteriaResolver;

String q = "age>5,age<10,age!=8,name==tuana9a,graduate@=prim;high;uni,year>=1991,year<=2003,deleted==false";
Criteria criteria = CriteriaResolver.resolve(q);
Criteria desiredCriteria = Criteria.where("age").gt(5).lt(10).ne(8)
        .and("name").is("tuana9a")
        .and("graduate").in("prim", "high", "uni")
        .and("year").gte(1991).lte(2003)
        .and("deleted").is(false);
Assertions.assertEquals(criteria.getCriteriaObject(), desiredCriteria.getCriteriaObject());

After you get your Criteria you can add it to the query and start querying data with it

criteria = criteria.and("location").near(point).maxDistance(maxDistance);
mongoTemplate.find(new Query(criteria), ParkingStation.class);

Resolve sort from string

// import com.tuana9a.spring.mongo.qrisolver.resolvers.SortResolver;

Sort sort = SortResolver.resolve("age=-1,address=1");
Sort desiredSort = Sort.by(Sort.Direction.DESC, "age").and(Sort.by(Sort.Direction.ASC, "address"));
Assertions.assertEquals(sort, desiredSort);

Global config

import com.tuana9a.spring.mongo.qrisolver.configs.Config;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class SetQueryResolverConfig implements CommandLineRunner {
    @Override
    public void run(String... args) {
        Config.REGEX_OPTIONS = "i";
    }
}

Improvement

Create an issue for it.

Report bug

Create an issue for it.

Contribution

Just open the PR and I will review it.

About

Resolve mongo criteria query from string

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published