Skip to content

Commit 0e99c45

Browse files
committed
Implement support for NOW variable #207
1 parent edc57ef commit 0e99c45

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/Expression.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,20 +1784,23 @@ static Object evaluate(Object expression, Document document) {
17841784
if (variableValue instanceof Missing) {
17851785
if (variableName.equals("$ROOT")) {
17861786
variableValue = document;
1787+
} else if (variableName.equals("$NOW")) {
1788+
variableValue = Instant.now();
17871789
} else {
17881790
throw new MongoServerError(17276, "Use of undefined variable: " + variableName.substring(1));
17891791
}
17901792
}
17911793

1794+
if (variableValue instanceof String) {
1795+
String variableValueString = (String) variableValue;
1796+
if (variableValueString.startsWith("$")) {
1797+
variableValue = evaluate(variableValue, document);
1798+
}
1799+
}
1800+
17921801
if (!value.equals(variableName)) {
17931802
String path = value.substring(variableName.length() + 1);
1794-
final Document evaluatedVariableValue;
1795-
if (variableValue instanceof Document) {
1796-
evaluatedVariableValue = (Document) variableValue;
1797-
} else {
1798-
evaluatedVariableValue = (Document) evaluate(variableValue, document);
1799-
}
1800-
return Utils.getSubdocumentValue(evaluatedVariableValue, path);
1803+
return Utils.getSubdocumentValue((Document) variableValue, path);
18011804
} else {
18021805
return variableValue;
18031806
}

test-common/src/main/java/de/bwaldvogel/mongo/backend/AbstractAggregationTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,6 +2501,23 @@ void testAggregateWithMerge_pipeline_newVariable() throws Exception {
25012501
.containsExactly(json("_id: 1, b: 'xyz', a: 1"));
25022502
}
25032503

2504+
@Test
2505+
void testAggregateWithMerge_pipeline_nowVariable() throws Exception {
2506+
collection.insertOne(json("_id: 1"));
2507+
2508+
MongoCollection<Document> otherCollection = db.getCollection("other");
2509+
otherCollection.insertOne(json("_id: 1"));
2510+
2511+
Instant instantBefore = Instant.now();
2512+
Document result = collection.aggregate(
2513+
jsonList("$merge: {into: 'other', let: {now: '$$NOW'}, whenMatched: [{$addFields: {timestamp: '$$now'}}]}")).first();
2514+
Instant instantAfter = Instant.now();
2515+
2516+
assertThat(result).containsOnlyKeys("_id", "timestamp");
2517+
System.out.println(result.get("timestamp"));
2518+
assertThat(result.getDate("timestamp")).isBetween(instantBefore, instantAfter);
2519+
}
2520+
25042521
private static Stream<Arguments> aggregateWithMerge_illegalParametersArguments() {
25052522
return Stream.of(
25062523
Arguments.of("$merge: null", IllegalStateException.class,

test-common/src/main/java/de/bwaldvogel/mongo/backend/AbstractTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package de.bwaldvogel.mongo.backend;
22

3+
import java.time.Instant;
34
import java.util.ArrayList;
5+
import java.util.Date;
46
import java.util.List;
57

68
import org.assertj.core.api.AbstractBooleanAssert;
9+
import org.assertj.core.api.AbstractDateAssert;
710
import org.assertj.core.api.AbstractDoubleAssert;
11+
import org.assertj.core.api.AbstractInstantAssert;
812
import org.assertj.core.api.AbstractIntegerAssert;
913
import org.assertj.core.api.AbstractLongAssert;
1014
import org.assertj.core.api.AbstractStringAssert;
@@ -170,6 +174,14 @@ protected static AbstractBooleanAssert<?> assertThat(Boolean actual) {
170174
return Assertions.assertThat(actual);
171175
}
172176

177+
protected static AbstractInstantAssert<?> assertThat(Instant actual) {
178+
return Assertions.assertThat(actual);
179+
}
180+
181+
protected static AbstractDateAssert<?> assertThat(Date actual) {
182+
return Assertions.assertThat(actual);
183+
}
184+
173185
protected static <T> IterableAssert<T> assertThat(Iterable<T> actual) {
174186
// improve assertion array by collection entire array
175187
List<T> values = TestUtils.toArray(actual);

0 commit comments

Comments
 (0)