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.

See Also:
  • Constructor Details

    • Expressions

      public Expressions()
  • Method Details

    • 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) )
       
    • getSpecification

      public <T> org.springframework.data.jpa.domain.Specification<T> getSpecification()
    • extractFields

      public static List<String> extractFields(Map<String,Object> expressions)
      Extracts all the field names (keys) from the current Expressions object, including nested fields within `$and` and `$or` compound operators.

      This method traverses the structure of the Expressions object recursively. If a compound operator (`$and` or `$or`) is encountered, it extracts fields from all nested expressions.

      Example: Given the following Expressions structure:

       {
           "firstName": "John",
           "$or": [
               {"lastName": "Doe"},
               {"age": {"$gt": 30}}
           ]
       }
       
      The resulting list of fields will be:
       ["firstName", "lastName", "age"]
       
      Returns:
      a list of field names present in the current Expressions object, including nested fields.