Add support for $tsIncrement aggregation operator.
See #4139 Original pull request: #4182.
This commit is contained in:
committed by
Mark Paluch
parent
714b23e0ce
commit
6a973b245f
@@ -818,6 +818,21 @@ public class DateOperators {
|
|||||||
return applyTimezone(DateFromString.fromString(dateReference()), timezone);
|
return applyTimezone(DateFromString.fromString(dateReference()), timezone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates new {@link AggregationExpression} that returns the incrementing ordinal from a timestamp.
|
||||||
|
*
|
||||||
|
* @return new instance of {@link TsIncrement}.
|
||||||
|
* @since 4.0
|
||||||
|
*/
|
||||||
|
public TsIncrement tsIncrement() {
|
||||||
|
|
||||||
|
if(timezone != null && !Timezone.none().equals(timezone)) {
|
||||||
|
throw new IllegalArgumentException("$tsIncrement does not support timezones");
|
||||||
|
}
|
||||||
|
|
||||||
|
return TsIncrement.tsIncrement(dateReference());
|
||||||
|
}
|
||||||
|
|
||||||
private Object dateReference() {
|
private Object dateReference() {
|
||||||
|
|
||||||
if (usesFieldRef()) {
|
if (usesFieldRef()) {
|
||||||
@@ -3182,6 +3197,63 @@ public class DateOperators {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link AggregationExpression} for {@code $tsIncrement}.
|
||||||
|
*
|
||||||
|
* @author Christoph Strobl
|
||||||
|
* @since 4.0
|
||||||
|
*/
|
||||||
|
public static class TsIncrement extends AbstractAggregationExpression {
|
||||||
|
|
||||||
|
private TsIncrement(Object value) {
|
||||||
|
super(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates new {@link TsIncrement} that returns the incrementing ordinal from a timestamp.
|
||||||
|
*
|
||||||
|
* @param value must not be {@literal null}.
|
||||||
|
* @return new instance of {@link TsIncrement}.
|
||||||
|
* @throws IllegalArgumentException if given {@literal value} is {@literal null}.
|
||||||
|
*/
|
||||||
|
public static TsIncrement tsIncrement(Object value) {
|
||||||
|
|
||||||
|
Assert.notNull(value, "Value must not be null");
|
||||||
|
return new TsIncrement(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates new {@link TsIncrement} that returns the incrementing ordinal from a timestamp.
|
||||||
|
*
|
||||||
|
* @param fieldReference must not be {@literal null}.
|
||||||
|
* @return new instance of {@link TsIncrement}.
|
||||||
|
* @throws IllegalArgumentException if given {@literal fieldReference} is {@literal null}.
|
||||||
|
*/
|
||||||
|
public static TsIncrement tsIncrementValueOf(String fieldReference) {
|
||||||
|
|
||||||
|
Assert.notNull(fieldReference, "FieldReference must not be null");
|
||||||
|
return tsIncrement(Fields.field(fieldReference));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates new {@link TsIncrement}.
|
||||||
|
*
|
||||||
|
* @param expression must not be {@literal null}.
|
||||||
|
* @return new instance of {@link TsIncrement}.
|
||||||
|
* @throws IllegalArgumentException if given {@literal expression} is {@literal null}.
|
||||||
|
*/
|
||||||
|
public static TsIncrement tsIncrementValueOf(AggregationExpression expression) {
|
||||||
|
|
||||||
|
Assert.notNull(expression, "Expression must not be null");
|
||||||
|
return tsIncrement(expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getMongoMethod() {
|
||||||
|
return "$tsIncrement";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface defining a temporal unit for date operators.
|
* Interface defining a temporal unit for date operators.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -191,6 +191,7 @@ public class MethodReferenceNode extends ExpressionNode {
|
|||||||
map.put("isoDayOfWeek", singleArgRef().forOperator("$isoDayOfWeek"));
|
map.put("isoDayOfWeek", singleArgRef().forOperator("$isoDayOfWeek"));
|
||||||
map.put("isoWeek", singleArgRef().forOperator("$isoWeek"));
|
map.put("isoWeek", singleArgRef().forOperator("$isoWeek"));
|
||||||
map.put("isoWeekYear", singleArgRef().forOperator("$isoWeekYear"));
|
map.put("isoWeekYear", singleArgRef().forOperator("$isoWeekYear"));
|
||||||
|
map.put("tsIncrement", singleArgRef().forOperator("$tsIncrement"));
|
||||||
|
|
||||||
// CONDITIONAL OPERATORS
|
// CONDITIONAL OPERATORS
|
||||||
map.put("cond", mapArgRef().forOperator("$cond") //
|
map.put("cond", mapArgRef().forOperator("$cond") //
|
||||||
|
|||||||
@@ -24,8 +24,6 @@ import java.time.temporal.ChronoUnit;
|
|||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.springframework.data.mongodb.core.aggregation.DateOperators.TemporalUnit;
|
|
||||||
import org.springframework.data.mongodb.core.aggregation.DateOperators.Timezone;
|
import org.springframework.data.mongodb.core.aggregation.DateOperators.Timezone;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -118,4 +116,18 @@ class DateOperatorsUnitTests {
|
|||||||
assertThat(DateOperators.zonedDateOf("purchaseDate", Timezone.valueOf("America/Chicago")).truncate("week").binSize(2).startOfWeek(DayOfWeek.MONDAY).toDocument(Aggregation.DEFAULT_CONTEXT))
|
assertThat(DateOperators.zonedDateOf("purchaseDate", Timezone.valueOf("America/Chicago")).truncate("week").binSize(2).startOfWeek(DayOfWeek.MONDAY).toDocument(Aggregation.DEFAULT_CONTEXT))
|
||||||
.isEqualTo("{ $dateTrunc: { date: \"$purchaseDate\", unit: \"week\", binSize: 2, startOfWeek : \"monday\", timezone : \"America/Chicago\" } }");
|
.isEqualTo("{ $dateTrunc: { date: \"$purchaseDate\", unit: \"week\", binSize: 2, startOfWeek : \"monday\", timezone : \"America/Chicago\" } }");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // GH-4139
|
||||||
|
void rendersTsIncrement() {
|
||||||
|
|
||||||
|
assertThat(DateOperators.dateOf("saleTimestamp").tsIncrement().toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo(
|
||||||
|
"{ $tsIncrement: \"$saleTimestamp\" }");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test // GH-4139
|
||||||
|
void tsIncrementErrorsOnTimezone() {
|
||||||
|
|
||||||
|
assertThatExceptionOfType(IllegalArgumentException.class)
|
||||||
|
.isThrownBy(() -> DateOperators.zonedDateOf("purchaseDate", Timezone.valueOf("America/Chicago")).tsIncrement());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1241,6 +1241,11 @@ public class SpelExpressionTransformerUnitTests {
|
|||||||
"sortArray(team, new org.bson.Document(\"name\" , 1))")).isEqualTo("{ $sortArray : { input : \"$team\", sortBy : {\"name\" : 1 } }}");
|
"sortArray(team, new org.bson.Document(\"name\" , 1))")).isEqualTo("{ $sortArray : { input : \"$team\", sortBy : {\"name\" : 1 } }}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // GH-4139
|
||||||
|
void shouldTsIncrement() {
|
||||||
|
assertThat(transform("tsIncrement(saleTimestamp)")).isEqualTo("{ $tsIncrement: \"$saleTimestamp\" }");
|
||||||
|
}
|
||||||
|
|
||||||
private Document transform(String expression, Object... params) {
|
private Document transform(String expression, Object... params) {
|
||||||
return (Document) transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);
|
return (Document) transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user