Package com.github.mhewedy.expressions
Class Expressions
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap<String,Object>
-
- com.github.mhewedy.expressions.Expressions
-
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<String,Object>
public class Expressions extends HashMap<String,Object>
Represents Group of expression using mongodb query api.Example:
{ "status": "A", "$or": [{ "qty": { "$lt": 30 } }, { "item": { "$in": ["A", "D"] } }] }
Support a list of Operators defined in
Operator
.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
-
Constructor Summary
Constructors Constructor Description Expressions()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Expressions
and(Expression expression)
Add the parameter expression to the list of expression in the expressions object.Expressions
or(Expression expression)
Create a new $or expression on the root, then adds to it the current expressions attached at the root and the expression passes as parameter.-
Methods inherited from class java.util.HashMap
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
-
-
-
-
Method Detail
-
or
public Expressions or(Expression expression)
Create a new $or expression on the root, then adds to it the current expressions attached at the root and the expression passes as parameter.Example: Suppose we have the following expression as json:
{"firstName": "ali"}
Then we added the following:
expressions.or(Expression.and( Expression.of("lastName", Operator.$eq, "ibrahim"), Expression.of("age", Operator.$gte, 10) ));
Then the output could be represented as:{ "$or": [ {"firstName": "ali"}, { "$and": [ {"lastName": "ibrahim"}, {"age": {"$gte": 10}}, ] } ] }
Or in sql as:where firstName = "ali" or lastName = "ibrahim" and age >= 10
-
and
public Expressions and(Expression expression)
Add the parameter expression to the list of expression in the expressions object.Example: Suppose we have the following expression as json:
{"firstName": "ali"}
Then we added the following:
expressions.and(Expression.of("birthDate", Operator.$gt, "1980-10-10")); expressions.and(Expression.or( Expression.of("lastName", Operator.$eq, "ibrahim"), Expression.of("age", Operator.$in, 10, 30) ));
Then the output could be represented as:{ "firstName": "ali", "birthDate": {"$gt": "1980-10-10"}, "$or": [ {"lastName": "ibrahim"}, {"age": {"$in": [10, 30]}} ] }
Or in sql as:where firstName = "ali" and birthDate > "1980-10-10" and (lastName = "ibrahim" or age in (10, 30) )
-
-