Class MathExpressions


  • public final class MathExpressions
    extends Object
    Defines helper methods for the math expressions
    Since:
    2.0
    • Constructor Detail

      • MathExpressions

        protected MathExpressions()
    • Method Detail

      • add

        public static MathExpression add​(Expression first,
                                         Expression... additional)
        Adds numbers together or adds numbers and a date. If one of the arguments is a date, $add treats the other arguments as milliseconds to add to the date.
        Parameters:
        first - the first expression to sum
        additional - any subsequent expressions to include in the sum
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $add
      • divide

        public static Expression divide​(Expression numerator,
                                        Expression divisor)
        Returns the result of dividing the first number by the second. Accepts two argument expressions.
        Parameters:
        numerator - the numerator
        divisor - the divisor
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $divide
      • mod

        public static Expression mod​(Expression dividend,
                                     Expression divisor)
        Returns the remainder of the first number divided by the second. Accepts two argument expressions.
        Parameters:
        dividend - the dividend
        divisor - the divisor
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $mod
      • multiply

        public static Expression multiply​(Expression first,
                                          Expression... additional)
        Multiplies numbers together and returns the result. Pass the arguments to $multiply in an array.
        Parameters:
        first - the first expression to add
        additional - any additional expressions
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $multiply
      • subtract

        public static Expression subtract​(Expression minuend,
                                          Expression subtrahend)
        Returns the result of subtracting the second value from the first. If the two values are numbers, return the difference. If the two values are dates, return the difference in milliseconds. If the two values are a date and a number in milliseconds, return the resulting date. Accepts two argument expressions. If the two values are a date and a number, specify the date argument first as it is not meaningful to subtract a date from a number.
        Parameters:
        minuend - the number to subtract from
        subtrahend - the number to subtract
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $subtract
      • trunc

        public static Expression trunc​(Expression number,
                                       Expression place)
        Truncates a number to a whole integer or to a specified decimal place.

        NOTE: Prior to 4.2, the place value wasn't available. Pass null if your server is older than 4.2.

        Parameters:
        number - the value
        place - the place to trunc to
        Returns:
        the new expression
        MongoDB documentation
        Aggregration Expression: $trunc