|
16 | 16 | package org.springframework.data.mongodb.core.aggregation;
|
17 | 17 |
|
18 | 18 | import java.util.Collections;
|
| 19 | +import java.util.HashMap; |
19 | 20 | import java.util.LinkedHashMap;
|
20 | 21 | import java.util.Map;
|
21 | 22 |
|
@@ -156,7 +157,7 @@ public static Timezone none() {
|
156 | 157 | * representing an Olson Timezone Identifier or UTC Offset.
|
157 | 158 | *
|
158 | 159 | * @param value the plain timezone {@link String}, a {@link Field} holding the timezone or an
|
159 |
| - * {@link AggregationExpression} resulting in the timezone. |
| 160 | + * {@link AggregationExpression} resulting in the timezone. |
160 | 161 | * @return new instance of {@link Timezone}.
|
161 | 162 | */
|
162 | 163 | public static Timezone valueOf(Object value) {
|
@@ -274,6 +275,41 @@ public DateOperatorFactory withTimezone(Timezone timezone) {
|
274 | 275 | return new DateOperatorFactory(fieldReference, expression, dateValue, timezone);
|
275 | 276 | }
|
276 | 277 |
|
| 278 | + /** |
| 279 | + * Creates new {@link AggregationExpression} that adds the value of the given {@link AggregationExpression |
| 280 | + * expression} (in {@literal units). @param expression must not be {@literal null}. |
| 281 | + * |
| 282 | + * @param unit the unit of measure. Must not be {@literal null}. |
| 283 | + * @return new instance of {@link DateAdd}. |
| 284 | + * @since 3.3 |
| 285 | + */ |
| 286 | + public DateAdd addValueOf(AggregationExpression expression, String unit) { |
| 287 | + return applyTimezone(DateAdd.addValueOf(expression, unit).toDate(dateReference()), timezone); |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * Creates new {@link AggregationExpression} that adds the value stored at the given {@literal field} (in |
| 292 | + * {@literal units). @param fieldReference must not be {@literal null}. |
| 293 | + * |
| 294 | + * @param unit the unit of measure. Must not be {@literal null}. |
| 295 | + * @return new instance of {@link DateAdd}. |
| 296 | + * @since 3.3 |
| 297 | + */ |
| 298 | + public DateAdd addValueOf(String fieldReference, String unit) { |
| 299 | + return applyTimezone(DateAdd.addValueOf(fieldReference, unit).toDate(dateReference()), timezone); |
| 300 | + } |
| 301 | + |
| 302 | + /** |
| 303 | + * Creates new {@link AggregationExpression} that adds the given value (in {@literal units). @param value must not |
| 304 | + * be {@literal null}. @param unit the unit of measure. Must not be {@literal null}. |
| 305 | + * |
| 306 | + * @return |
| 307 | + * @since 3.3 new instance of {@link DateAdd}. |
| 308 | + */ |
| 309 | + public DateAdd add(Object value, String unit) { |
| 310 | + return applyTimezone(DateAdd.addValue(value, unit).toDate(dateReference()), timezone); |
| 311 | + } |
| 312 | + |
277 | 313 | /**
|
278 | 314 | * Creates new {@link AggregationExpression} that returns the day of the year for a date as a number between 1 and
|
279 | 315 | * 366.
|
@@ -1480,7 +1516,6 @@ protected java.util.Map<String, Object> append(String key, Object value) {
|
1480 | 1516 | } else {
|
1481 | 1517 | clone.put("timezone", ((Timezone) value).value);
|
1482 | 1518 | }
|
1483 |
| - |
1484 | 1519 | } else {
|
1485 | 1520 | clone.put(key, value);
|
1486 | 1521 | }
|
@@ -1911,7 +1946,7 @@ default T millisecondOf(AggregationExpression expression) {
|
1911 | 1946 | * @author Matt Morrissette
|
1912 | 1947 | * @author Christoph Strobl
|
1913 | 1948 | * @see <a href=
|
1914 |
| - * "https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromParts/">https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromParts/</a> |
| 1949 | + * "https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromParts/">https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromParts/</a> |
1915 | 1950 | * @since 2.1
|
1916 | 1951 | */
|
1917 | 1952 | public static class DateFromParts extends TimezonedDateAggregationExpression implements DateParts<DateFromParts> {
|
@@ -2086,7 +2121,7 @@ default DateFromParts yearOf(AggregationExpression expression) {
|
2086 | 2121 | * @author Matt Morrissette
|
2087 | 2122 | * @author Christoph Strobl
|
2088 | 2123 | * @see <a href=
|
2089 |
| - * "https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromParts/">https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromParts/</a> |
| 2124 | + * "https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromParts/">https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromParts/</a> |
2090 | 2125 | * @since 2.1
|
2091 | 2126 | */
|
2092 | 2127 | public static class IsoDateFromParts extends TimezonedDateAggregationExpression
|
@@ -2262,7 +2297,7 @@ default IsoDateFromParts isoWeekYearOf(AggregationExpression expression) {
|
2262 | 2297 | * @author Matt Morrissette
|
2263 | 2298 | * @author Christoph Strobl
|
2264 | 2299 | * @see <a href=
|
2265 |
| - * "https://docs.mongodb.com/manual/reference/operator/aggregation/dateToParts/">https://docs.mongodb.com/manual/reference/operator/aggregation/dateToParts/</a> |
| 2300 | + * "https://docs.mongodb.com/manual/reference/operator/aggregation/dateToParts/">https://docs.mongodb.com/manual/reference/operator/aggregation/dateToParts/</a> |
2266 | 2301 | * @since 2.1
|
2267 | 2302 | */
|
2268 | 2303 | public static class DateToParts extends TimezonedDateAggregationExpression {
|
@@ -2343,7 +2378,7 @@ protected String getMongoMethod() {
|
2343 | 2378 | * @author Matt Morrissette
|
2344 | 2379 | * @author Christoph Strobl
|
2345 | 2380 | * @see <a href=
|
2346 |
| - * "https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromString/">https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromString/</a> |
| 2381 | + * "https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromString/">https://docs.mongodb.com/manual/reference/operator/aggregation/dateFromString/</a> |
2347 | 2382 | * @since 2.1
|
2348 | 2383 | */
|
2349 | 2384 | public static class DateFromString extends TimezonedDateAggregationExpression {
|
@@ -2418,6 +2453,103 @@ protected String getMongoMethod() {
|
2418 | 2453 | }
|
2419 | 2454 | }
|
2420 | 2455 |
|
| 2456 | + /** |
| 2457 | + * {@link AggregationExpression} for {@code $dateAdd}.<br /> |
| 2458 | + * <strong>NOTE:</strong> Requires MongoDB 5.0 or later. |
| 2459 | + * |
| 2460 | + * @author Christoph Strobl |
| 2461 | + * @since 3.3 |
| 2462 | + */ |
| 2463 | + public static class DateAdd extends TimezonedDateAggregationExpression { |
| 2464 | + |
| 2465 | + private DateAdd(Object value) { |
| 2466 | + super(value); |
| 2467 | + } |
| 2468 | + |
| 2469 | + /** |
| 2470 | + * Add the number of {@literal units} of the result of the given {@link AggregationExpression expression} to a |
| 2471 | + * {@link #toDate(Object) start date}. |
| 2472 | + * |
| 2473 | + * @param expression must not be {@literal null}. |
| 2474 | + * @param unit must not be {@literal null}. |
| 2475 | + * @return new instance of {@link DateAdd}. |
| 2476 | + */ |
| 2477 | + public static DateAdd addValueOf(AggregationExpression expression, String unit) { |
| 2478 | + return addValue(expression, unit); |
| 2479 | + } |
| 2480 | + |
| 2481 | + /** |
| 2482 | + * Add the number of {@literal units} from a {@literal field} to a {@link #toDate(Object) start date}. |
| 2483 | + * |
| 2484 | + * @param fieldReference must not be {@literal null}. |
| 2485 | + * @param unit must not be {@literal null}. |
| 2486 | + * @return new instance of {@link DateAdd}. |
| 2487 | + */ |
| 2488 | + public static DateAdd addValueOf(String fieldReference, String unit) { |
| 2489 | + return addValue(Fields.field(fieldReference), unit); |
| 2490 | + } |
| 2491 | + |
| 2492 | + /** |
| 2493 | + * Add the number of {@literal units} to a {@link #toDate(Object) start date}. |
| 2494 | + * |
| 2495 | + * @param value must not be {@literal null}. |
| 2496 | + * @param unit must not be {@literal null}. |
| 2497 | + * @return new instance of {@link DateAdd}. |
| 2498 | + */ |
| 2499 | + public static DateAdd addValue(Object value, String unit) { |
| 2500 | + |
| 2501 | + Map<String, Object> args = new HashMap<>(); |
| 2502 | + args.put("unit", unit); |
| 2503 | + args.put("amount", value); |
| 2504 | + return new DateAdd(args); |
| 2505 | + } |
| 2506 | + |
| 2507 | + /** |
| 2508 | + * Define the start date, in UTC, for the addition operation. |
| 2509 | + * |
| 2510 | + * @param expression must not be {@literal null}. |
| 2511 | + * @return new instance of {@link DateAdd}. |
| 2512 | + */ |
| 2513 | + public DateAdd toDateOf(AggregationExpression expression) { |
| 2514 | + return toDate(expression); |
| 2515 | + } |
| 2516 | + |
| 2517 | + /** |
| 2518 | + * Define the start date, in UTC, for the addition operation. |
| 2519 | + * |
| 2520 | + * @param fieldReference must not be {@literal null}. |
| 2521 | + * @return new instance of {@link DateAdd}. |
| 2522 | + */ |
| 2523 | + public DateAdd toDateOf(String fieldReference) { |
| 2524 | + return toDate(Fields.field(fieldReference)); |
| 2525 | + } |
| 2526 | + |
| 2527 | + /** |
| 2528 | + * Define the start date, in UTC, for the addition operation. |
| 2529 | + * |
| 2530 | + * @param dateExpression anything that evaluates to a valid date. Must not be {@literal null}. |
| 2531 | + * @return new instance of {@link DateAdd}. |
| 2532 | + */ |
| 2533 | + public DateAdd toDate(Object dateExpression) { |
| 2534 | + return new DateAdd(append("startDate", dateExpression)); |
| 2535 | + } |
| 2536 | + |
| 2537 | + /** |
| 2538 | + * Optionally set the {@link Timezone} to use. If not specified {@literal UTC} is used. |
| 2539 | + * |
| 2540 | + * @param timezone must not be {@literal null}. Consider {@link Timezone#none()} instead. |
| 2541 | + * @return new instance of {@link DateAdd}. |
| 2542 | + */ |
| 2543 | + public DateAdd withTimezone(Timezone timezone) { |
| 2544 | + return new DateAdd(appendTimezone(argumentMap(), timezone)); |
| 2545 | + } |
| 2546 | + |
| 2547 | + @Override |
| 2548 | + protected String getMongoMethod() { |
| 2549 | + return "$dateAdd"; |
| 2550 | + } |
| 2551 | + } |
| 2552 | + |
2421 | 2553 | @SuppressWarnings("unchecked")
|
2422 | 2554 | private static <T extends TimezonedDateAggregationExpression> T applyTimezone(T instance, Timezone timezone) {
|
2423 | 2555 | return !ObjectUtils.nullSafeEquals(Timezone.none(), timezone) && !instance.hasTimezone()
|
|
0 commit comments