|
24 | 24 | import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.StdDevPop;
|
25 | 25 | import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.StdDevSamp;
|
26 | 26 | import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.Sum;
|
| 27 | +import org.springframework.lang.Nullable; |
27 | 28 | import org.springframework.util.Assert;
|
| 29 | +import org.springframework.util.StringUtils; |
28 | 30 |
|
29 | 31 | /**
|
30 | 32 | * Gateway to {@literal Arithmetic} aggregation operations that perform math operations on numbers.
|
@@ -532,6 +534,31 @@ public Round roundToPlace(int place) {
|
532 | 534 | return round().place(place);
|
533 | 535 | }
|
534 | 536 |
|
| 537 | + /** |
| 538 | + * Creates new {@link AggregationExpression} that calculates the mathematical derivative value. |
| 539 | + * |
| 540 | + * @return new instance of {@link Derivative}. |
| 541 | + * @since 3.3 |
| 542 | + */ |
| 543 | + public Derivative derivative() { |
| 544 | + return derivative(null); |
| 545 | + } |
| 546 | + |
| 547 | + /** |
| 548 | + * Creates new {@link AggregationExpression} that calculates the mathematical derivative value. |
| 549 | + * |
| 550 | + * @param unit The time unit ({@literal week, day, hour, minute, second, millisecond}) to apply can be |
| 551 | + * {@literal null}. |
| 552 | + * @return new instance of {@link Derivative}. |
| 553 | + * @since 3.3 |
| 554 | + */ |
| 555 | + public Derivative derivative(@Nullable String unit) { |
| 556 | + |
| 557 | + Derivative derivative = usesFieldRef() ? Derivative.derivativeOf(fieldReference) |
| 558 | + : Derivative.derivativeOf(expression); |
| 559 | + return StringUtils.hasText(unit) ? derivative.unit(unit) : derivative; |
| 560 | + } |
| 561 | + |
535 | 562 | private boolean usesFieldRef() {
|
536 | 563 | return fieldReference != null;
|
537 | 564 | }
|
@@ -1665,4 +1692,32 @@ protected String getMongoMethod() {
|
1665 | 1692 | return "$round";
|
1666 | 1693 | }
|
1667 | 1694 | }
|
| 1695 | + |
| 1696 | + public static class Derivative extends AbstractAggregationExpression { |
| 1697 | + |
| 1698 | + private Derivative(Object value) { |
| 1699 | + super(value); |
| 1700 | + } |
| 1701 | + |
| 1702 | + public static Derivative derivativeOf(String fieldReference) { |
| 1703 | + return new Derivative(Collections.singletonMap("input", Fields.field(fieldReference))); |
| 1704 | + } |
| 1705 | + |
| 1706 | + public static Derivative derivativeOf(AggregationExpression expression) { |
| 1707 | + return new Derivative(Collections.singletonMap("input", expression)); |
| 1708 | + } |
| 1709 | + |
| 1710 | + public static Derivative derivativeOfValue(Number value) { |
| 1711 | + return new Derivative(Collections.singletonMap("input", value)); |
| 1712 | + } |
| 1713 | + |
| 1714 | + public Derivative unit(String unit) { |
| 1715 | + return new Derivative(append("unit", unit)); |
| 1716 | + } |
| 1717 | + |
| 1718 | + @Override |
| 1719 | + protected String getMongoMethod() { |
| 1720 | + return "$derivative"; |
| 1721 | + } |
| 1722 | + } |
1668 | 1723 | }
|
0 commit comments