Skip to content

Commit 8d223ab

Browse files
christophstroblmp911de
authored andcommitted
Add support for $tsSecond aggregation operator.
See #4139 Original pull request: #4182.
1 parent 6a973b2 commit 8d223ab

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/DateOperators.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,21 @@ public TsIncrement tsIncrement() {
833833
return TsIncrement.tsIncrement(dateReference());
834834
}
835835

836+
/**
837+
* Creates new {@link AggregationExpression} that returns the seconds from a timestamp.
838+
*
839+
* @return new instance of {@link TsIncrement}.
840+
* @since 4.0
841+
*/
842+
public TsSecond tsSecond() {
843+
844+
if(timezone != null && !Timezone.none().equals(timezone)) {
845+
throw new IllegalArgumentException("$tsSecond does not support timezones");
846+
}
847+
848+
return TsSecond.tsSecond(dateReference());
849+
}
850+
836851
private Object dateReference() {
837852

838853
if (usesFieldRef()) {
@@ -3254,6 +3269,63 @@ protected String getMongoMethod() {
32543269
}
32553270
}
32563271

3272+
/**
3273+
* {@link AggregationExpression} for {@code $tsSecond}.
3274+
*
3275+
* @author Christoph Strobl
3276+
* @since 4.0
3277+
*/
3278+
public static class TsSecond extends AbstractAggregationExpression {
3279+
3280+
private TsSecond(Object value) {
3281+
super(value);
3282+
}
3283+
3284+
/**
3285+
* Creates new {@link TsSecond} that returns the incrementing ordinal from a timestamp.
3286+
*
3287+
* @param value must not be {@literal null}.
3288+
* @return new instance of {@link TsSecond}.
3289+
* @throws IllegalArgumentException if given {@literal value} is {@literal null}.
3290+
*/
3291+
public static TsSecond tsSecond(Object value) {
3292+
3293+
Assert.notNull(value, "Value must not be null");
3294+
return new TsSecond(value);
3295+
}
3296+
3297+
/**
3298+
* Creates new {@link TsSecond} that returns the incrementing ordinal from a timestamp.
3299+
*
3300+
* @param fieldReference must not be {@literal null}.
3301+
* @return new instance of {@link TsSecond}.
3302+
* @throws IllegalArgumentException if given {@literal fieldReference} is {@literal null}.
3303+
*/
3304+
public static TsSecond tsSecondValueOf(String fieldReference) {
3305+
3306+
Assert.notNull(fieldReference, "FieldReference must not be null");
3307+
return tsSecond(Fields.field(fieldReference));
3308+
}
3309+
3310+
/**
3311+
* Creates new {@link TsSecond}.
3312+
*
3313+
* @param expression must not be {@literal null}.
3314+
* @return new instance of {@link TsSecond}.
3315+
* @throws IllegalArgumentException if given {@literal expression} is {@literal null}.
3316+
*/
3317+
public static TsSecond tsSecondValueOf(AggregationExpression expression) {
3318+
3319+
Assert.notNull(expression, "Expression must not be null");
3320+
return tsSecond(expression);
3321+
}
3322+
3323+
@Override
3324+
protected String getMongoMethod() {
3325+
return "$tsSecond";
3326+
}
3327+
}
3328+
32573329
/**
32583330
* Interface defining a temporal unit for date operators.
32593331
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ public class MethodReferenceNode extends ExpressionNode {
192192
map.put("isoWeek", singleArgRef().forOperator("$isoWeek"));
193193
map.put("isoWeekYear", singleArgRef().forOperator("$isoWeekYear"));
194194
map.put("tsIncrement", singleArgRef().forOperator("$tsIncrement"));
195+
map.put("tsSecond", singleArgRef().forOperator("$tsSecond"));
195196

196197
// CONDITIONAL OPERATORS
197198
map.put("cond", mapArgRef().forOperator("$cond") //

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/DateOperatorsUnitTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,18 @@ void tsIncrementErrorsOnTimezone() {
130130
assertThatExceptionOfType(IllegalArgumentException.class)
131131
.isThrownBy(() -> DateOperators.zonedDateOf("purchaseDate", Timezone.valueOf("America/Chicago")).tsIncrement());
132132
}
133+
134+
@Test // GH-4139
135+
void rendersTsSecond() {
136+
137+
assertThat(DateOperators.dateOf("saleTimestamp").tsSecond().toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo(
138+
"{ $tsSecond: \"$saleTimestamp\" }");
139+
}
140+
141+
@Test // GH-4139
142+
void tsSecondErrorsOnTimezone() {
143+
144+
assertThatExceptionOfType(IllegalArgumentException.class)
145+
.isThrownBy(() -> DateOperators.zonedDateOf("purchaseDate", Timezone.valueOf("America/Chicago")).tsSecond());
146+
}
133147
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SpelExpressionTransformerUnitTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,11 @@ void shouldRenderSortArray() {
12451245
void shouldTsIncrement() {
12461246
assertThat(transform("tsIncrement(saleTimestamp)")).isEqualTo("{ $tsIncrement: \"$saleTimestamp\" }");
12471247
}
1248+
1249+
@Test // GH-4139
1250+
void shouldTsSecond() {
1251+
assertThat(transform("tsSecond(saleTimestamp)")).isEqualTo("{ $tsSecond: \"$saleTimestamp\" }");
1252+
}
12481253

12491254
private Document transform(String expression, Object... params) {
12501255
return (Document) transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);

0 commit comments

Comments
 (0)