@@ -340,6 +340,42 @@ public DayOfWeek dayOfWeek() {
340
340
return applyTimezone (DayOfWeek .dayOfWeek (dateReference ()), timezone );
341
341
}
342
342
343
+ /**
344
+ * Creates new {@link AggregationExpression} that calculates the difference (in {@literal units) to the date
345
+ * computed by the given {@link AggregationExpression expression}. @param expression must not be {@literal null}.
346
+ *
347
+ * @param unit the unit of measure. Must not be {@literal null}.
348
+ * @return new instance of {@link DateAdd}.
349
+ * @since 3.3
350
+ */
351
+ public DateDiff diffValueOf (AggregationExpression expression , String unit ) {
352
+ return applyTimezone (DateDiff .diffValueOf (expression , unit ).toDate (dateReference ()), timezone );
353
+ }
354
+
355
+ /**
356
+ * Creates new {@link AggregationExpression} that calculates the difference (in {@literal units) to the date stored
357
+ * at the given {@literal field}. @param expression must not be {@literal null}.
358
+ *
359
+ * @param unit the unit of measure. Must not be {@literal null}.
360
+ * @return new instance of {@link DateAdd}.
361
+ * @since 3.3
362
+ */
363
+ public DateDiff diffValueOf (String fieldReference , String unit ) {
364
+ return applyTimezone (DateDiff .diffValueOf (fieldReference , unit ).toDate (dateReference ()), timezone );
365
+ }
366
+
367
+ /**
368
+ * Creates new {@link AggregationExpression} that calculates the difference (in {@literal units) to the date given
369
+ * {@literal value}. @param value anything the resolves to a valid date. Must not be {@literal null}.
370
+ *
371
+ * @param unit the unit of measure. Must not be {@literal null}.
372
+ * @return new instance of {@link DateAdd}.
373
+ * @since 3.3
374
+ */
375
+ public DateDiff diff (Object value , String unit ) {
376
+ return applyTimezone (DateDiff .diffValue (value , unit ).toDate (dateReference ()), timezone );
377
+ }
378
+
343
379
/**
344
380
* Creates new {@link AggregationExpression} that returns the year portion of a date.
345
381
*
@@ -2550,6 +2586,114 @@ protected String getMongoMethod() {
2550
2586
}
2551
2587
}
2552
2588
2589
+ /**
2590
+ * {@link AggregationExpression} for {@code $dateDiff}.<br />
2591
+ * <strong>NOTE:</strong> Requires MongoDB 5.0 or later.
2592
+ *
2593
+ * @author Christoph Strobl
2594
+ * @since 3.3
2595
+ */
2596
+ public static class DateDiff extends TimezonedDateAggregationExpression {
2597
+
2598
+ private DateDiff (Object value ) {
2599
+ super (value );
2600
+ }
2601
+
2602
+ /**
2603
+ * Add the number of {@literal units} of the result of the given {@link AggregationExpression expression} to a
2604
+ * {@link #toDate(Object) start date}.
2605
+ *
2606
+ * @param expression must not be {@literal null}.
2607
+ * @param unit must not be {@literal null}.
2608
+ * @return new instance of {@link DateAdd}.
2609
+ */
2610
+ public static DateDiff diffValueOf (AggregationExpression expression , String unit ) {
2611
+ return diffValue (expression , unit );
2612
+ }
2613
+
2614
+ /**
2615
+ * Add the number of {@literal units} from a {@literal field} to a {@link #toDate(Object) start date}.
2616
+ *
2617
+ * @param fieldReference must not be {@literal null}.
2618
+ * @param unit must not be {@literal null}.
2619
+ * @return new instance of {@link DateAdd}.
2620
+ */
2621
+ public static DateDiff diffValueOf (String fieldReference , String unit ) {
2622
+ return diffValue (Fields .field (fieldReference ), unit );
2623
+ }
2624
+
2625
+ /**
2626
+ * Add the number of {@literal units} to a {@link #toDate(Object) start date}.
2627
+ *
2628
+ * @param value must not be {@literal null}.
2629
+ * @param unit must not be {@literal null}.
2630
+ * @return new instance of {@link DateAdd}.
2631
+ */
2632
+ public static DateDiff diffValue (Object value , String unit ) {
2633
+
2634
+ Map <String , Object > args = new HashMap <>();
2635
+ args .put ("unit" , unit );
2636
+ args .put ("endDate" , value );
2637
+ return new DateDiff (args );
2638
+ }
2639
+
2640
+ /**
2641
+ * Define the start date, in UTC, for the addition operation.
2642
+ *
2643
+ * @param expression must not be {@literal null}.
2644
+ * @return new instance of {@link DateAdd}.
2645
+ */
2646
+ public DateDiff toDateOf (AggregationExpression expression ) {
2647
+ return toDate (expression );
2648
+ }
2649
+
2650
+ /**
2651
+ * Define the start date, in UTC, for the addition operation.
2652
+ *
2653
+ * @param fieldReference must not be {@literal null}.
2654
+ * @return new instance of {@link DateAdd}.
2655
+ */
2656
+ public DateDiff toDateOf (String fieldReference ) {
2657
+ return toDate (Fields .field (fieldReference ));
2658
+ }
2659
+
2660
+ /**
2661
+ * Define the start date, in UTC, for the addition operation.
2662
+ *
2663
+ * @param dateExpression anything that evaluates to a valid date. Must not be {@literal null}.
2664
+ * @return new instance of {@link DateAdd}.
2665
+ */
2666
+ public DateDiff toDate (Object dateExpression ) {
2667
+ return new DateDiff (append ("startDate" , dateExpression ));
2668
+ }
2669
+
2670
+ /**
2671
+ * Optionally set the {@link Timezone} to use. If not specified {@literal UTC} is used.
2672
+ *
2673
+ * @param timezone must not be {@literal null}. Consider {@link Timezone#none()} instead.
2674
+ * @return new instance of {@link DateAdd}.
2675
+ */
2676
+ public DateDiff withTimezone (Timezone timezone ) {
2677
+ return new DateDiff (appendTimezone (argumentMap (), timezone ));
2678
+ }
2679
+
2680
+ /**
2681
+ * Set the start day of the week if the unit if measure is set to {@literal week}. Uses {@literal Sunday} by
2682
+ * default.
2683
+ *
2684
+ * @param day must not be {@literal null}.
2685
+ * @return new instance of {@link DateDiff}.
2686
+ */
2687
+ public DateDiff startOfWeek (Object day ) {
2688
+ return new DateDiff (append ("startOfWeek" , day ));
2689
+ }
2690
+
2691
+ @ Override
2692
+ protected String getMongoMethod () {
2693
+ return "$dateDiff" ;
2694
+ }
2695
+ }
2696
+
2553
2697
@ SuppressWarnings ("unchecked" )
2554
2698
private static <T extends TimezonedDateAggregationExpression > T applyTimezone (T instance , Timezone timezone ) {
2555
2699
return !ObjectUtils .nullSafeEquals (Timezone .none (), timezone ) && !instance .hasTimezone ()
0 commit comments