Skip to content

Commit 7ec258b

Browse files
sebge2emaspherechristophstrobl
authored andcommitted
DATAMONGO-1530 - Add support for missing MongoDB 3.2 aggregation pipeline operators.
Original Pull Request: #410
1 parent a47a119 commit 7ec258b

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/spel/MethodReferenceNode.java

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,89 @@
1515
*/
1616
package org.springframework.data.mongodb.core.spel;
1717

18+
import org.springframework.expression.spel.ExpressionState;
19+
import org.springframework.expression.spel.ast.MethodReference;
20+
1821
import java.util.Collections;
1922
import java.util.HashMap;
2023
import java.util.Map;
2124

22-
import org.springframework.expression.spel.ExpressionState;
23-
import org.springframework.expression.spel.ast.MethodReference;
24-
2525
/**
2626
* An {@link ExpressionNode} representing a method reference.
27-
*
27+
*
2828
* @author Oliver Gierke
2929
* @author Thomas Darimont
30+
* @author Sebastien Gerard
3031
*/
3132
public class MethodReferenceNode extends ExpressionNode {
3233

3334
private static final Map<String, String> FUNCTIONS;
3435

3536
static {
36-
3737
Map<String, String> map = new HashMap<String, String>();
3838

39+
map.put("and", "$and"); // Returns true only when all its expressions evaluate to true.
40+
map.put("or", "$or"); // Returns true when any of its expressions evaluates to true.
41+
map.put("not", "$not"); // Returns the boolean value that is the opposite of its argument expression.
42+
43+
map.put("setEquals", "$setEquals"); // Returns true if the input sets have the same distinct elements.
44+
map.put("setIntersection", "$setIntersection"); // Returns a set with elements that appear in all of the input sets.
45+
map.put("setUnion", "$setUnion"); // Returns a set with elements that appear in any of the input sets.
46+
map.put("setDifference", "$setDifference"); // Returns a set with elements that appear in the 1st set but not in the
47+
// 2nd.
48+
map.put("setIsSubset", "$setIsSubset"); // Returns true if all elements of the 1st set appear in the 2nd set.
49+
map.put("anyElementTrue", "$anyElementTrue"); // Returns whether any elements of a set evaluate to true.
50+
map.put("allElementsTrue", "$allElementsTrue"); // Returns whether no element of a set evaluates to false.
51+
52+
map.put("cmp", "$cmp"); // Returns: 0 if the two values are equivalent, 1 if the first value is greater than the
53+
// second, and -1 if the first value is less than the second.
54+
map.put("eq", "$eq"); // Returns true if the values are equivalent.
55+
map.put("gt", "$gt"); // Returns true if the first value is greater than the second.
56+
map.put("gte", "$gte"); // Returns true if the first value is greater than or equal to the second.
57+
map.put("lt", "$lt"); // Returns true if the first value is less than the second.
58+
map.put("lte", "$lte"); // Returns true if the first value is less than or equal to the second.
59+
map.put("ne", "$ne"); // Returns true if the values are not equivalent.
60+
61+
map.put("abs", "$abs"); // Returns the absolute value of a number.;
62+
map.put("add", "$add"); // Adds numbers to return the sum, or adds numbers and a date to return a new date.
63+
map.put("ceil", "$ceil"); // Returns the smallest integer greater than or equal to the specified number.
64+
map.put("divide", "$divide"); // Returns the result of dividing the first number by the second.
65+
map.put("exp", "$exp"); // Raises e to the specified exponent.
66+
map.put("floor", "$floor"); // Returns the largest integer less than or equal to the specified number.
67+
map.put("ln", "$ln"); // Calculates the natural log of a number.
68+
map.put("log", "$log"); // Calculates the log of a number in the specified base.
69+
map.put("log10", "$log10"); // Calculates the log base 10 of a number.
70+
map.put("mod", "$mod"); // Returns the remainder of the first number divided by the second.
71+
map.put("multiply", "$multiply"); // Multiplies numbers to return the product.
72+
map.put("pow", "$pow"); // Raises a number to the specified exponent.
73+
map.put("sqrt", "$sqrt"); // Calculates the square root.
74+
map.put("subtract", "$subtract"); // Returns the result of subtracting the second value from the first. If the
75+
// two values are numbers, return the difference. If the two values are dates, return the difference in
76+
// milliseconds.
77+
map.put("trunc", "$trunc"); // Truncates a number to its integer.
78+
3979
map.put("concat", "$concat"); // Concatenates two strings.
40-
map.put("strcasecmp", "$strcasecmp"); // Compares two strings and returns an integer that reflects the comparison.
4180
map.put("substr", "$substr"); // Takes a string and returns portion of that string.
4281
map.put("toLower", "$toLower"); // Converts a string to lowercase.
4382
map.put("toUpper", "$toUpper"); // Converts a string to uppercase.
83+
map.put("strcasecmp", "$strcasecmp"); // Compares two strings and returns an integer that reflects the comparison.
84+
85+
map.put("meta", "$meta"); // Access text search metadata.
86+
87+
map.put("arrayElemAt", "$arrayElemAt"); // Returns the element at the specified array index.
88+
map.put("concatArrays", "$concatArrays"); // Concatenates arrays to return the concatenated array.
89+
map.put("filter", "$filter"); // Selects a subset of the array to return an array with only the elements that
90+
// match the filter condition.
91+
map.put("isArray", "$isArray"); // Determines if the operand is an array. Returns a boolean.
92+
map.put("size", "$size"); // Returns the number of elements in the array.
93+
map.put("slice", "$slice"); // Returns a subset of an array.
94+
95+
map.put("map", "$map"); // Applies a subexpression to each element of an array and returns the array of
96+
// resulting values in order.
97+
map.put("let", "$let"); // Defines variables for use within the scope of a subexpression and returns the result
98+
// of the subexpression.
99+
100+
map.put("literal", "$literal"); // Return a value without parsing.
44101

45102
map.put("dayOfYear", "$dayOfYear"); // Converts a date to a number between 1 and 366.
46103
map.put("dayOfMonth", "$dayOfMonth"); // Converts a date to a number between 1 and 31.
@@ -53,6 +110,13 @@ public class MethodReferenceNode extends ExpressionNode {
53110
map.put("second", "$second"); // Converts a date into a number between 0 and 59. May be 60 to account for leap
54111
// seconds.
55112
map.put("millisecond", "$millisecond"); // Returns the millisecond portion of a date as an integer between 0 and
113+
// 999.
114+
map.put("dateToString", "$dateToString"); // Returns the date as a formatted string.
115+
116+
map.put("cond", "$cond"); // A ternary operator that evaluates one expression, and depending on the result,
117+
// returns the value of one of the other two expressions.
118+
map.put("ifNull", "$ifNull"); // Returns either the non-null result of the first expression or the result of the
119+
// second expression if the first expression results in a null result.
56120

57121
FUNCTIONS = Collections.unmodifiableMap(map);
58122
}
@@ -63,11 +127,8 @@ public class MethodReferenceNode extends ExpressionNode {
63127

64128
/**
65129
* Returns the name of the method.
66-
*
67-
* @return
68130
*/
69131
public String getMethodName() {
70-
71132
String name = getName();
72133
String methodName = name.substring(0, name.indexOf('('));
73134
return FUNCTIONS.get(methodName);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/spel/OperatorNode.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
/**
3131
* An {@link ExpressionNode} representing an operator.
32-
*
32+
*
3333
* @author Oliver Gierke
3434
* @author Thomas Darimont
3535
*/
@@ -47,14 +47,18 @@ public class OperatorNode extends ExpressionNode {
4747
map.put("/", "$divide");
4848
map.put("%", "$mod");
4949

50+
map.put("and", "and");
51+
map.put("or", "or");
52+
map.put("!", "not");
53+
5054
OPERATORS = Collections.unmodifiableMap(map);
5155
}
5256

5357
private final Operator operator;
5458

5559
/**
5660
* Creates a new {@link OperatorNode} from the given {@link Operator} and {@link ExpressionState}.
57-
*
61+
*
5862
* @param node must not be {@literal null}.
5963
* @param state must not be {@literal null}.
6064
*/

0 commit comments

Comments
 (0)