Class Expression

java.lang.Object
net.pincette.mongo.Expression

public class Expression extends Object
This class lets you apply a MongoDB expression to a JSON object. The following operators are not supported: $indexOfBytes , $strLenBytes, $substrBytes, $toDate, $toObjectId and the date expression operators. Accumulators are also not supported. Only the aggregation variables $$NOW and $$ROOT are supported. The extension variable $$TODAY returns the current date as an ISO 8601 string.

The $unescape extension operator converts key names that start with "#$" to "$". This way an expression can be escaped from implementation generation.

The expression of the $jslt extension operator should be an object with the fields input and script. The former is an expression that should produce a JSON value. If it is absent $$ROOT will be assumed. The latter is a reference to a JSLT script. If the value starts with "resource:" then it is treated as a resource in the class path. Otherwise, it is a filename or a script. The result of the expression will be a JSON value. If the expression is just a string, then it will ba handled as a script value.

The $sort extension operator receives an object with the mandatory field input, which should be an expression that yields an array. The optional field direction can have the values "asc" or "desc", the former being the default. The optional field paths is a list of field paths. When it is present only object values in the array are considered. They will be sorted hierarchically with the values extracted with the paths.

The operator $elemMatch, which is normally part of the MongoDB query language, is available in another form. Its value must be an array with two elements. The first element is an expression that yields an array and the second is an $elemMatch specification. If behaves as the projection variant of the operator.

If you wrap an expression in the $trace operator then tracing will be done for it in the logger "net.pincette.mongo.expressions" at level INFO.

Since:
1.2
Author:
Werner Donné
  • Method Details

    • function

      public static Function<JsonObject,JsonValue> function(JsonValue expression)
      Constructs a function with expression.
      Parameters:
      expression - the MongoDB expression.
      Returns:
      The function, which is stateless.
      Since:
      1.2
    • function

      public static Function<JsonObject,JsonValue> function(JsonValue expression, Features features)
      Constructs a function with expression.
      Parameters:
      expression - the MongoDB expression.
      features - extra features. It may be null.
      Returns:
      The function, which is stateless.
      Since:
      2.0
    • function

      public static Function<JsonObject,JsonValue> function(JsonValue expression, Map<String,JsonValue> variables)
      Constructs a function with expression.
      Parameters:
      expression - the MongoDB expression.
      variables - external variables. It may be null.
      Returns:
      The function, which is stateless.
      Since:
      1.3
    • function

      public static Function<JsonObject,JsonValue> function(JsonValue expression, Map<String,JsonValue> variables, Features features)
      Constructs a function with expression.
      Parameters:
      expression - the MongoDB expression.
      variables - external variables. It may be null.
      features - extra features. It may be null.
      Returns:
      The function, which is stateless.
      Since:
      2.0
    • function

      public static Function<JsonObject,JsonValue> function(Bson expression)
      Constructs a function with expression.
      Parameters:
      expression - the MongoDB expression.
      Returns:
      The function, which is stateless.
      Since:
      1.2
    • function

      public static Function<JsonObject,JsonValue> function(Bson expression, Features features)
      Constructs a function with expression.
      Parameters:
      expression - the MongoDB expression.
      features - extra features. It may be null.
      Returns:
      The function, which is stateless.
      Since:
      2.0
    • function

      public static Function<JsonObject,JsonValue> function(Bson expression, Map<String,JsonValue> variables)
      Constructs a function with expression.
      Parameters:
      expression - the MongoDB expression.
      variables - external variables.
      Returns:
      The function, which is stateless.
      Since:
      1.3
    • function

      public static Function<JsonObject,JsonValue> function(Bson expression, Map<String,JsonValue> variables, Features features)
      Constructs a function with expression.
      Parameters:
      expression - the MongoDB expression.
      variables - external variables.
      features - extra features. It may be null.
      Returns:
      The function, which is stateless.
      Since:
      2.0
    • implementation

      public static Implementation implementation(JsonValue expression, Features features)
      Extension developers can use this to delegate implementation generation to subexpressions.
      Parameters:
      expression - the given expression.
      features - extra features. It may be null.
      Returns:
      The generated implementation.
      Since:
      2.0
    • logger

      public static Logger logger()
      If you set the log level to FINEST you will get a trace.
      Returns:
      The logger with the name "net.pincette.mongo.expression".
      Since:
      1.3
    • memberFunction

      public static Implementation memberFunction(JsonValue expression, String name, Features features)
      If expression is an object, which has a subexpression called name, then this method returns the implementation of the subexpression.
      Parameters:
      expression - the given expression.
      name - the name of the subexpression.
      features - extra features. It may be null.
      Returns:
      The implementation of the subexpression. If expression is not an object or if the subexpression is invalid null will be returned.
      Since:
      2.0
    • memberFunctions

      public static List<Implementation> memberFunctions(JsonValue expression, String name, Features features)
      If expression is an object, which has a subexpression called name, and if the subexpression is actually an array of subexpressions, then this method returns the implementation of the subexpressions.
      Parameters:
      expression - the given expression.
      name - the name of the subexpressions.
      features - extra features. It may be null.
      Returns:
      The implementation of the subexpressions. If expression is not an object or if the subexpressions are invalid null will be returned.
      Since:
      2.0
    • replaceVariables

      public static JsonValue replaceVariables(JsonValue expression, Map<String,JsonValue> variables)
      Replaces all variables in expression. Variable names start with "$$".
      Parameters:
      expression - the given expression.
      variables - maps variable names to JSON values.
      Returns:
      The new expression.
      Since:
      1.3.2