|
16 | 16 | package org.springframework.data.mongodb.core.convert;
|
17 | 17 |
|
18 | 18 | import static org.springframework.data.mongodb.core.DocumentTestUtils.*;
|
| 19 | +import static org.springframework.data.mongodb.core.aggregation.AggregationExpressionCriteria.*; |
19 | 20 | import static org.springframework.data.mongodb.core.query.Criteria.*;
|
20 | 21 | import static org.springframework.data.mongodb.core.query.Query.*;
|
21 | 22 | import static org.springframework.data.mongodb.test.util.Assertions.*;
|
|
43 | 44 | import org.springframework.data.geo.Point;
|
44 | 45 | import org.springframework.data.mongodb.core.DocumentTestUtils;
|
45 | 46 | import org.springframework.data.mongodb.core.Person;
|
| 47 | +import org.springframework.data.mongodb.core.aggregation.ComparisonOperators; |
46 | 48 | import org.springframework.data.mongodb.core.aggregation.ConditionalOperators;
|
47 | 49 | import org.springframework.data.mongodb.core.aggregation.EvaluationOperators;
|
| 50 | +import org.springframework.data.mongodb.core.aggregation.EvaluationOperators.Expr; |
48 | 51 | import org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext;
|
49 | 52 | import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
|
50 | 53 | import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
|
@@ -1461,6 +1464,51 @@ void considersValueConverterWhenPresent() {
|
1461 | 1464 | assertThat(mappedObject).isEqualTo(new org.bson.Document("text", "eulav"));
|
1462 | 1465 | }
|
1463 | 1466 |
|
| 1467 | + @Test // GH-2750 |
| 1468 | + void mapsAggregationExpression() { |
| 1469 | + |
| 1470 | + Query query = query(whereExpr(ComparisonOperators.valueOf("field").greaterThan("budget"))); |
| 1471 | + org.bson.Document mappedObject = mapper.getMappedObject(query.getQueryObject(), |
| 1472 | + context.getPersistentEntity(CustomizedField.class)); |
| 1473 | + assertThat(mappedObject).isEqualTo("{ $expr : { $gt : [ '$foo', '$budget'] } }"); |
| 1474 | + } |
| 1475 | + |
| 1476 | + @Test // GH-2750 |
| 1477 | + void unwrapsAggregationExpressionExprObjectWrappedInExpressionCriteria() { |
| 1478 | + |
| 1479 | + Query query = query(whereExpr(Expr.valueOf(ComparisonOperators.valueOf("field").greaterThan("budget")))); |
| 1480 | + org.bson.Document mappedObject = mapper.getMappedObject(query.getQueryObject(), |
| 1481 | + context.getPersistentEntity(CustomizedField.class)); |
| 1482 | + assertThat(mappedObject).isEqualTo("{ $expr : { $gt : [ '$foo', '$budget'] } }"); |
| 1483 | + } |
| 1484 | + |
| 1485 | + @Test // GH-2750 |
| 1486 | + void mapsMongoExpressionToFieldsIfItsAnAggregationExpression() { |
| 1487 | + |
| 1488 | + Query query = query(expr(ComparisonOperators.valueOf("field").greaterThan("budget"))); |
| 1489 | + org.bson.Document mappedObject = mapper.getMappedObject(query.getQueryObject(), |
| 1490 | + context.getPersistentEntity(CustomizedField.class)); |
| 1491 | + assertThat(mappedObject).isEqualTo("{ $expr : { $gt : [ '$foo', '$budget'] } }"); |
| 1492 | + } |
| 1493 | + |
| 1494 | + @Test // GH-2750 |
| 1495 | + void usageOfMongoExpressionOnCriteriaDoesNotUnwrapAnExprAggregationExpression() { |
| 1496 | + |
| 1497 | + Query query = query(expr(Expr.valueOf(ComparisonOperators.valueOf("field").greaterThan("budget")))); |
| 1498 | + org.bson.Document mappedObject = mapper.getMappedObject(query.getQueryObject(), |
| 1499 | + context.getPersistentEntity(CustomizedField.class)); |
| 1500 | + assertThat(mappedObject).isEqualTo("{ $expr : { $expr : { $gt : [ '$foo', '$budget'] } } }"); |
| 1501 | + } |
| 1502 | + |
| 1503 | + @Test // GH-2750 |
| 1504 | + void usesMongoExpressionDocumentAsIsIfItIsNotAnAggregationExpression() { |
| 1505 | + |
| 1506 | + Query query = query(expr(() -> org.bson.Document.parse("{ $gt : [ '$field', '$budget'] }"))); |
| 1507 | + org.bson.Document mappedObject = mapper.getMappedObject(query.getQueryObject(), |
| 1508 | + context.getPersistentEntity(CustomizedField.class)); |
| 1509 | + assertThat(mappedObject).isEqualTo("{ $expr : { $gt : [ '$field', '$budget'] } }"); |
| 1510 | + } |
| 1511 | + |
1464 | 1512 | class WithDeepArrayNesting {
|
1465 | 1513 |
|
1466 | 1514 | List<WithNestedArray> level0;
|
|
0 commit comments