@@ -79,7 +79,7 @@ public static class ArrayOperatorFactory {
79
79
80
80
private final @ Nullable String fieldReference ;
81
81
private final @ Nullable AggregationExpression expression ;
82
- private final @ Nullable Collection values ;
82
+ private final @ Nullable Collection <?> values ;
83
83
84
84
/**
85
85
* Creates new {@link ArrayOperatorFactory} for given {@literal fieldReference}.
@@ -214,6 +214,10 @@ public AsBuilder filter() {
214
214
return Filter .filter (fieldReference );
215
215
}
216
216
217
+ if (usesExpression ()) {
218
+ return Filter .filter (expression );
219
+ }
220
+
217
221
Assert .state (values != null , "Values must not be null" );
218
222
return Filter .filter (new ArrayList <>(values ));
219
223
}
@@ -317,7 +321,8 @@ public ArrayOperatorFactory.ReduceInitialValueBuilder reduce(PropertyExpression.
317
321
}
318
322
319
323
/**
320
- * Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given {@link Sort order}.
324
+ * Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given {@link Sort
325
+ * order}.
321
326
*
322
327
* @return new instance of {@link SortArray}.
323
328
* @since 4.0
@@ -397,8 +402,8 @@ public First first() {
397
402
}
398
403
399
404
/**
400
- * Creates new {@link AggregationExpression} that return the last element in the given array.
401
- * <strong>NOTE:</strong> Requires MongoDB 4.4 or later.
405
+ * Creates new {@link AggregationExpression} that return the last element in the given array. <strong>NOTE:</strong>
406
+ * Requires MongoDB 4.4 or later.
402
407
*
403
408
* @return new instance of {@link Last}.
404
409
* @since 3.4
@@ -649,6 +654,19 @@ public static AsBuilder filter(Field field) {
649
654
return new FilterExpressionBuilder ().filter (field );
650
655
}
651
656
657
+ /**
658
+ * Set the {@link AggregationExpression} resolving to an arry to apply the {@code $filter} to.
659
+ *
660
+ * @param expression must not be {@literal null}.
661
+ * @return never {@literal null}.
662
+ * @since 4.2
663
+ */
664
+ public static AsBuilder filter (AggregationExpression expression ) {
665
+
666
+ Assert .notNull (expression , "Field must not be null" );
667
+ return new FilterExpressionBuilder ().filter (expression );
668
+ }
669
+
652
670
/**
653
671
* Set the {@literal values} to apply the {@code $filter} to.
654
672
*
@@ -681,7 +699,16 @@ private Document toFilter(ExposedFields exposedFields, AggregationOperationConte
681
699
}
682
700
683
701
private Object getMappedInput (AggregationOperationContext context ) {
684
- return input instanceof Field field ? context .getReference (field ).toString () : input ;
702
+
703
+ if (input instanceof Field field ) {
704
+ return context .getReference (field ).toString ();
705
+ }
706
+
707
+ if (input instanceof AggregationExpression expression ) {
708
+ return expression .toDocument (context );
709
+ }
710
+
711
+ return input ;
685
712
}
686
713
687
714
private Object getMappedCondition (AggregationOperationContext context ) {
@@ -715,6 +742,15 @@ public interface InputBuilder {
715
742
* @return
716
743
*/
717
744
AsBuilder filter (Field field );
745
+
746
+ /**
747
+ * Set the {@link AggregationExpression} resolving to an array to apply the {@code $filter} to.
748
+ *
749
+ * @param expression must not be {@literal null}.
750
+ * @return
751
+ * @since 4.1.1
752
+ */
753
+ AsBuilder filter (AggregationExpression expression );
718
754
}
719
755
720
756
/**
@@ -797,6 +833,14 @@ public AsBuilder filter(Field field) {
797
833
return this ;
798
834
}
799
835
836
+ @ Override
837
+ public AsBuilder filter (AggregationExpression expression ) {
838
+
839
+ Assert .notNull (expression , "Expression must not be null" );
840
+ filter .input = expression ;
841
+ return this ;
842
+ }
843
+
800
844
@ Override
801
845
public ConditionBuilder as (String variableName ) {
802
846
@@ -1333,7 +1377,7 @@ public Reduce reduce(PropertyExpression... expressions) {
1333
1377
Assert .notNull (expressions , "PropertyExpressions must not be null" );
1334
1378
1335
1379
return new Reduce (Fields .field (fieldReference ), initialValue ,
1336
- Arrays .<AggregationExpression >asList (expressions ));
1380
+ Arrays .<AggregationExpression > asList (expressions ));
1337
1381
}
1338
1382
};
1339
1383
}
@@ -1690,7 +1734,7 @@ public Zip zip(Object... arrays) {
1690
1734
* @author Christoph Strobl
1691
1735
* @author Shashank Sharma
1692
1736
* @see <a href=
1693
- * "https://docs.mongodb.com/manual/reference/operator/aggregation/in/">https://docs.mongodb.com/manual/reference/operator/aggregation/in/</a>
1737
+ * "https://docs.mongodb.com/manual/reference/operator/aggregation/in/">https://docs.mongodb.com/manual/reference/operator/aggregation/in/</a>
1694
1738
* @since 2.2
1695
1739
*/
1696
1740
public static class In extends AbstractAggregationExpression {
@@ -1779,7 +1823,7 @@ public interface InBuilder {
1779
1823
*
1780
1824
* @author Christoph Strobl
1781
1825
* @see <a href=
1782
- * "https://docs.mongodb.com/manual/reference/operator/aggregation/arrayToObject/">https://docs.mongodb.com/manual/reference/operator/aggregation/arrayToObject/</a>
1826
+ * "https://docs.mongodb.com/manual/reference/operator/aggregation/arrayToObject/">https://docs.mongodb.com/manual/reference/operator/aggregation/arrayToObject/</a>
1783
1827
* @since 2.1
1784
1828
*/
1785
1829
public static class ArrayToObject extends AbstractAggregationExpression {
@@ -1976,7 +2020,7 @@ public static SortArray sortArrayOf(AggregationExpression expression) {
1976
2020
1977
2021
/**
1978
2022
* Set the order to put elements in.
1979
- *
2023
+ *
1980
2024
* @param sort must not be {@literal null}.
1981
2025
* @return new instance of {@link SortArray}.
1982
2026
*/
0 commit comments