Skip to content

Commit 80c3b52

Browse files
committed
Implement date expressions
1 parent 4099900 commit 80c3b52

File tree

3 files changed

+226
-0
lines changed

3 files changed

+226
-0
lines changed

driver-core/src/main/com/mongodb/client/model/expressions/DateExpression.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,18 @@
2020
* Expresses a date value.
2121
*/
2222
public interface DateExpression extends Expression {
23+
IntegerExpression year();
24+
IntegerExpression month();
25+
IntegerExpression dayOfMonth();
26+
IntegerExpression dayOfWeek();
27+
IntegerExpression dayOfYear();
28+
IntegerExpression hour();
29+
IntegerExpression minute();
30+
IntegerExpression second();
31+
IntegerExpression week();
32+
IntegerExpression millisecond();
33+
34+
StringExpression dateToString();
35+
StringExpression dateToString(StringExpression format, StringExpression timezone);
2336

2437
}

driver-core/src/main/com/mongodb/client/model/expressions/MqlExpression.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,71 @@ public IntegerExpression min(final IntegerExpression i) {
332332
return new MqlExpression<>(ast("$min", i));
333333
}
334334

335+
/** @see DateExpression */
336+
337+
@Override
338+
public IntegerExpression year() {
339+
return new MqlExpression<>(ast("$year"));
340+
}
341+
342+
@Override
343+
public IntegerExpression month() {
344+
return new MqlExpression<>(ast("$month"));
345+
}
346+
347+
@Override
348+
public IntegerExpression dayOfMonth() {
349+
return new MqlExpression<>(ast("$dayOfMonth"));
350+
}
351+
352+
@Override
353+
public IntegerExpression dayOfWeek() {
354+
return new MqlExpression<>(ast("$dayOfWeek"));
355+
}
356+
357+
@Override
358+
public IntegerExpression dayOfYear() {
359+
return new MqlExpression<>(ast("$dayOfYear"));
360+
}
361+
362+
@Override
363+
public IntegerExpression hour() {
364+
return new MqlExpression<>(ast("$hour"));
365+
}
366+
367+
@Override
368+
public IntegerExpression minute() {
369+
return new MqlExpression<>(ast("$minute"));
370+
}
371+
372+
@Override
373+
public IntegerExpression second() {
374+
return new MqlExpression<>(ast("$second"));
375+
}
376+
377+
@Override
378+
public IntegerExpression week() {
379+
return new MqlExpression<>(ast("$week"));
380+
}
381+
382+
@Override
383+
public IntegerExpression millisecond() {
384+
return new MqlExpression<>(ast("$millisecond"));
385+
}
386+
387+
@Override
388+
public StringExpression dateToString() {
389+
return newMqlExpression((cr) -> astDoc("$dateToString", new BsonDocument()
390+
.append("date", this.toBsonValue(cr))));
391+
}
392+
393+
@Override
394+
public StringExpression dateToString(final StringExpression format, final StringExpression timezone) {
395+
return newMqlExpression((cr) -> astDoc("$dateToString", new BsonDocument()
396+
.append("date", this.toBsonValue(cr))
397+
.append("format", extractBsonValue(cr, format))
398+
.append("timezone", extractBsonValue(cr, timezone))));
399+
}
335400

336401
/** @see StringExpression */
337402

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.client.model.expressions;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import java.time.Instant;
22+
23+
import static com.mongodb.client.model.expressions.Expressions.of;
24+
25+
class DateExpressionsFunctionalTest extends AbstractExpressionsFunctionalTest {
26+
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/#date-expression-operators
27+
28+
private final Instant instant = Instant.parse("2007-12-03T10:15:30.005Z");
29+
30+
@Test
31+
public void literalsTest() {
32+
assertExpression(
33+
instant,
34+
of(instant),
35+
"{'$date': '2007-12-03T10:15:30.005Z'}");
36+
}
37+
38+
@Test
39+
public void yearTest() {
40+
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/year/
41+
assertExpression(
42+
2007,
43+
of(instant).year(),
44+
"{'$year': {'$date': '2007-12-03T10:15:30.005Z'}}");
45+
}
46+
47+
@Test
48+
public void monthTest() {
49+
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/month/
50+
assertExpression(
51+
12,
52+
of(instant).month(),
53+
"{'$month': {'$date': '2007-12-03T10:15:30.005Z'}}");
54+
}
55+
56+
@Test
57+
public void dayOfMonthTest() {
58+
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfMonth/
59+
assertExpression(
60+
3,
61+
of(instant).dayOfMonth(),
62+
"{'$dayOfMonth': {'$date': '2007-12-03T10:15:30.005Z'}}");
63+
}
64+
65+
@Test
66+
public void dayOfWeekTest() {
67+
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfWeek/
68+
assertExpression(
69+
2,
70+
of(instant).dayOfWeek(),
71+
"{'$dayOfWeek': {'$date': '2007-12-03T10:15:30.005Z'}}");
72+
}
73+
74+
@Test
75+
public void dayOfYearTest() {
76+
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfYear/
77+
assertExpression(
78+
337,
79+
of(instant).dayOfYear(),
80+
"{'$dayOfYear': {'$date': '2007-12-03T10:15:30.005Z'}}");
81+
}
82+
83+
@Test
84+
public void hourTest() {
85+
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/hour/
86+
assertExpression(
87+
10,
88+
of(instant).hour(),
89+
"{'$hour': {'$date': '2007-12-03T10:15:30.005Z'}}");
90+
}
91+
92+
@Test
93+
public void minuteTest() {
94+
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/minute/
95+
assertExpression(
96+
15,
97+
of(instant).minute(),
98+
"{'$minute': {'$date': '2007-12-03T10:15:30.005Z'}}");
99+
}
100+
101+
@Test
102+
public void secondTest() {
103+
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/second/
104+
assertExpression(
105+
30,
106+
of(instant).second(),
107+
"{'$second': {'$date': '2007-12-03T10:15:30.005Z'}}");
108+
}
109+
110+
@Test
111+
public void weekTest() {
112+
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/week/
113+
assertExpression(
114+
48,
115+
of(instant).week(),
116+
"{'$week': {'$date': '2007-12-03T10:15:30.005Z'}}");
117+
}
118+
119+
@Test
120+
public void millisecondTest() {
121+
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/millisecond/
122+
assertExpression(
123+
5,
124+
of(instant).millisecond(),
125+
"{'$millisecond': {'$date': '2007-12-03T10:15:30.005Z'}}");
126+
}
127+
128+
@Test
129+
public void dateToStringTest() {
130+
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateToString/
131+
assertExpression(
132+
"2007-12-03T10:15:30.005Z",
133+
of(instant).dateToString());
134+
// with parameters
135+
assertExpression(
136+
"2007-12-03T05:15:30.005Z",
137+
of(instant).dateToString(of("%Y-%m-%dT%H:%M:%S.%LZ"), of("America/New_York")),
138+
"{'$dateToString': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, "
139+
+ "'format': '%Y-%m-%dT%H:%M:%S.%LZ', "
140+
+ "'timezone': 'America/New_York'}}");
141+
assertExpression(
142+
"2007-12-03T14:45:30.005Z",
143+
of(instant).dateToString(of("%Y-%m-%dT%H:%M:%S.%LZ"), of("+04:30")),
144+
"{'$dateToString': {'date': {'$date': '2007-12-03T10:15:30.005Z'}, "
145+
+ "'format': '%Y-%m-%dT%H:%M:%S.%LZ', "
146+
+ "'timezone': '+04:30'}}");
147+
}
148+
}

0 commit comments

Comments
 (0)