Add support for $tsSecond aggregation operator.
See #4139 Original pull request: #4182.
This commit is contained in:
committed by
Mark Paluch
parent
6a973b245f
commit
8d223abd05
@@ -833,6 +833,21 @@ public class DateOperators {
|
||||
return TsIncrement.tsIncrement(dateReference());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new {@link AggregationExpression} that returns the seconds from a timestamp.
|
||||
*
|
||||
* @return new instance of {@link TsIncrement}.
|
||||
* @since 4.0
|
||||
*/
|
||||
public TsSecond tsSecond() {
|
||||
|
||||
if(timezone != null && !Timezone.none().equals(timezone)) {
|
||||
throw new IllegalArgumentException("$tsSecond does not support timezones");
|
||||
}
|
||||
|
||||
return TsSecond.tsSecond(dateReference());
|
||||
}
|
||||
|
||||
private Object dateReference() {
|
||||
|
||||
if (usesFieldRef()) {
|
||||
@@ -3254,6 +3269,63 @@ public class DateOperators {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link AggregationExpression} for {@code $tsSecond}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since 4.0
|
||||
*/
|
||||
public static class TsSecond extends AbstractAggregationExpression {
|
||||
|
||||
private TsSecond(Object value) {
|
||||
super(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new {@link TsSecond} that returns the incrementing ordinal from a timestamp.
|
||||
*
|
||||
* @param value must not be {@literal null}.
|
||||
* @return new instance of {@link TsSecond}.
|
||||
* @throws IllegalArgumentException if given {@literal value} is {@literal null}.
|
||||
*/
|
||||
public static TsSecond tsSecond(Object value) {
|
||||
|
||||
Assert.notNull(value, "Value must not be null");
|
||||
return new TsSecond(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new {@link TsSecond} that returns the incrementing ordinal from a timestamp.
|
||||
*
|
||||
* @param fieldReference must not be {@literal null}.
|
||||
* @return new instance of {@link TsSecond}.
|
||||
* @throws IllegalArgumentException if given {@literal fieldReference} is {@literal null}.
|
||||
*/
|
||||
public static TsSecond tsSecondValueOf(String fieldReference) {
|
||||
|
||||
Assert.notNull(fieldReference, "FieldReference must not be null");
|
||||
return tsSecond(Fields.field(fieldReference));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new {@link TsSecond}.
|
||||
*
|
||||
* @param expression must not be {@literal null}.
|
||||
* @return new instance of {@link TsSecond}.
|
||||
* @throws IllegalArgumentException if given {@literal expression} is {@literal null}.
|
||||
*/
|
||||
public static TsSecond tsSecondValueOf(AggregationExpression expression) {
|
||||
|
||||
Assert.notNull(expression, "Expression must not be null");
|
||||
return tsSecond(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getMongoMethod() {
|
||||
return "$tsSecond";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface defining a temporal unit for date operators.
|
||||
*
|
||||
|
||||
@@ -192,6 +192,7 @@ public class MethodReferenceNode extends ExpressionNode {
|
||||
map.put("isoWeek", singleArgRef().forOperator("$isoWeek"));
|
||||
map.put("isoWeekYear", singleArgRef().forOperator("$isoWeekYear"));
|
||||
map.put("tsIncrement", singleArgRef().forOperator("$tsIncrement"));
|
||||
map.put("tsSecond", singleArgRef().forOperator("$tsSecond"));
|
||||
|
||||
// CONDITIONAL OPERATORS
|
||||
map.put("cond", mapArgRef().forOperator("$cond") //
|
||||
|
||||
@@ -130,4 +130,18 @@ class DateOperatorsUnitTests {
|
||||
assertThatExceptionOfType(IllegalArgumentException.class)
|
||||
.isThrownBy(() -> DateOperators.zonedDateOf("purchaseDate", Timezone.valueOf("America/Chicago")).tsIncrement());
|
||||
}
|
||||
|
||||
@Test // GH-4139
|
||||
void rendersTsSecond() {
|
||||
|
||||
assertThat(DateOperators.dateOf("saleTimestamp").tsSecond().toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo(
|
||||
"{ $tsSecond: \"$saleTimestamp\" }");
|
||||
}
|
||||
|
||||
@Test // GH-4139
|
||||
void tsSecondErrorsOnTimezone() {
|
||||
|
||||
assertThatExceptionOfType(IllegalArgumentException.class)
|
||||
.isThrownBy(() -> DateOperators.zonedDateOf("purchaseDate", Timezone.valueOf("America/Chicago")).tsSecond());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1245,6 +1245,11 @@ public class SpelExpressionTransformerUnitTests {
|
||||
void shouldTsIncrement() {
|
||||
assertThat(transform("tsIncrement(saleTimestamp)")).isEqualTo("{ $tsIncrement: \"$saleTimestamp\" }");
|
||||
}
|
||||
|
||||
@Test // GH-4139
|
||||
void shouldTsSecond() {
|
||||
assertThat(transform("tsSecond(saleTimestamp)")).isEqualTo("{ $tsSecond: \"$saleTimestamp\" }");
|
||||
}
|
||||
|
||||
private Document transform(String expression, Object... params) {
|
||||
return (Document) transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);
|
||||
|
||||
Reference in New Issue
Block a user