Giter Site home page Giter Site logo

rsql-mongodb's Introduction

Defunct

This library has been superseded by the more general (and just better) rest-query-engine.

Java CI with Maven

What

An open source implementation for converting rsql to mongo queries for spring data. RSQL is a flexible and readable query language based off of apache's FIQL. Providing this adapater for mongo queries makes RSQL a natural choice for APIs that want to provide flexible querying and whose underlying datastore is mongodb.

How to use it

Add maven dependency

<dependency>
    <groupId>com.github.borsch</groupId>
    <artifactId>rsql-mongodb</artifactId>
    <version>1.1</version>
</dependency>
@Autowired
private MongoOperations mongoOperations;

@Autowired
private MongoMappingContext mappingContext;

// build the converter. You can define a list of conversions instead, but using a spring conversion service and mongo mapping context
// works really quite well. Some applications will have a conversion service available from the application context that can include
// custom converters (iso -> date, etc)
ComparisonToCriteriaConverter converter = new ComparisonToCriteriaConverter(new DefaultConversionService(), mongoMappingContext);

// build the actual rsql string -> mongo criteria adapter
RsqlMongoAdapter adapter = new RsqlMongoAdapter(converter);

// convert the rsql to some criteria and add that criteria to the mongo query object
Query query = Query.query(adapter.getCriteria("firstName==joe", Person.class));

// make your query!
List<Person> personsNamedJoe = mongoOperations.find(query, Person.class);

Examples of supported cases. For the full list, please see tests.

# basic equality
firstName==joe -> { "firstName" : "joe"}

# basic inequality
firstName!=joe -> { "firstName" : { "$ne" : "joe"}}

# basic greater than
createDate=gt=300 -> { "createDate" : { "$gt" : 300}}

# basic greater than or equal
createDate=ge=300 -> { "createDate" : { "$gte" : 300}}

# basic less than
createDate=lt=300 -> { "createDate" : { "$lt" : 300}}

# basic less than or equal
createDate=le=300 -> { "createDate" : { "$lte" : 300}}

# basic element appears in list
firstName=in=(billy,bob,joel) -> { "firstName" : { "$in" : [ "billy" , "bob" , "joel"]}}

# basic element does not appear in list
firstName=out=(billy,bob,joel) -> { "firstName" : { "$nin" : [ "billy" , "bob" , "joel"]}}

# anding of two basic conditions
firstName!=joe;lastName==dummy -> { "$and" : [ { "firstName" : { "$ne" : "joe"}} , { "lastName" : "dummy"}]}

# oring of two basic conditions
firstName!=john,lastName==doe -> { "$or" : [ { "firstName" : { "$ne" : "john"}} , { "lastName" : "doe"}]}

# anding of two oring conditions of anding conditions
((firstName==john;lastName==doe),(firstName==aaron;lastName==carter));((age==21;height==90),(age==30;height==100)) -> 

    {
       "$and":[
          {
             "$or":[
                {
                   "$and":[
                      {
                         "firstName":"john"
                      },
                      {
                         "lastName":"doe"
                      }
                   ]
                },
                {
                   "$and":[
                      {
                         "firstName":"aaron"
                      },
                      {
                         "lastName":"carter"
                      }
                   ]
                }
             ]
          },
          {
             "$or":[
                {
                   "$and":[
                      {
                         "age":21
                      },
                      {
                         "height":90
                      }
                   ]
                },
                {
                   "$and":[
                      {
                         "age":30
                      },
                      {
                         "height":100
                      }
                   ]
                }
             ]
          }
       ]
    }
    

License

This project is licensed under MIT license.

rsql-mongodb's People

Contributors

rutledgepaulv avatar borsch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.