Skip to content

aam229/java-jongo-query

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Jongo Query

Module to simplify the generation of Jongo queries programmatically. The module supports arrays and objects that contain JSON templates. It keeps track of both the JSON template and the parameters necessary to build it.

Below are some examples of how it can be used.

Object example

Let's transform the following code:

if(name != null) {
  collection.find("{age: {$gt: #}, name: #}", 21, name);
} else {
  collection.find("{age: {$gt: #}}", 21);
}

Using the jongo-query module, you can rewrite this as:

ObjectCriteria query = new ObjectCriteria();
query.add("age: {$gt: #}", 21);
if(name != null){
  query.add("name: #", name);
}
collection.find(query.getQuery(), query.getParameters());

Array example

It is difficult to generate a query from a list of values in jongo. The ArrayCriteria class addresses that issue.

List<Person> persons = ...;
ArrayCriteria query = new ArrayCriteria();
for(Person person: people){
  query.add("{age: {$gt: #}, name: #}", person.getAge(), person.getName());
}
collection.find(String.format("{$or: %s}", query.getQuery()), query.getParameters());

It would generate a query that looks like {$or: [{age: {$gt: #}, name: #}, {age: {$gt: #}, name: #} ...]} with the appropriate number of parameters.

About

Generate Jongo queries programmatically

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages