@@ -78,7 +78,7 @@ public static class ArrayOperatorFactory {
78
78
79
79
private final @ Nullable String fieldReference ;
80
80
private final @ Nullable AggregationExpression expression ;
81
- private final @ Nullable Collection values ;
81
+ private final @ Nullable Collection <?> values ;
82
82
83
83
/**
84
84
* Creates new {@link ArrayOperatorFactory} for given {@literal fieldReference}.
@@ -213,6 +213,10 @@ public AsBuilder filter() {
213
213
return Filter .filter (fieldReference );
214
214
}
215
215
216
+ if (usesExpression ()) {
217
+ return Filter .filter (expression );
218
+ }
219
+
216
220
Assert .state (values != null , "Values must not be null!" );
217
221
return Filter .filter (new ArrayList <>(values ));
218
222
}
@@ -363,7 +367,7 @@ public ArrayToObject toObject() {
363
367
364
368
return usesExpression () ? ArrayToObject .arrayValueOfToObject (expression ) : ArrayToObject .arrayToObject (values );
365
369
}
366
-
370
+
367
371
/**
368
372
* Creates new {@link AggregationExpression} that return the first element in the associated array.
369
373
* <strong>NOTE:</strong> Requires MongoDB 4.4 or later.
@@ -379,10 +383,10 @@ public First first() {
379
383
380
384
return usesExpression () ? First .firstOf (expression ) : First .first (values );
381
385
}
382
-
386
+
383
387
/**
384
- * Creates new {@link AggregationExpression} that return the last element in the given array.
385
- * <strong>NOTE:</strong> Requires MongoDB 4.4 or later.
388
+ * Creates new {@link AggregationExpression} that return the last element in the given array. <strong>NOTE:</strong>
389
+ * Requires MongoDB 4.4 or later.
386
390
*
387
391
* @return new instance of {@link Last}.
388
392
* @since 3.4
@@ -633,6 +637,19 @@ public static AsBuilder filter(Field field) {
633
637
return new FilterExpressionBuilder ().filter (field );
634
638
}
635
639
640
+ /**
641
+ * Set the {@link AggregationExpression} resolving to an arry to apply the {@code $filter} to.
642
+ *
643
+ * @param expression must not be {@literal null}.
644
+ * @return never {@literal null}.
645
+ * @since 4.2
646
+ */
647
+ public static AsBuilder filter (AggregationExpression expression ) {
648
+
649
+ Assert .notNull (expression , "Field must not be null" );
650
+ return new FilterExpressionBuilder ().filter (expression );
651
+ }
652
+
636
653
/**
637
654
* Set the {@literal values} to apply the {@code $filter} to.
638
655
*
@@ -669,7 +686,16 @@ private Document toFilter(ExposedFields exposedFields, AggregationOperationConte
669
686
}
670
687
671
688
private Object getMappedInput (AggregationOperationContext context ) {
672
- return input instanceof Field ? context .getReference ((Field ) input ).toString () : input ;
689
+
690
+ if (input instanceof Field ) {
691
+ return context .getReference ((Field ) input ).toString ();
692
+ }
693
+
694
+ if (input instanceof AggregationExpression ) {
695
+ return ((AggregationExpression ) input ).toDocument (context );
696
+ }
697
+
698
+ return input ;
673
699
}
674
700
675
701
private Object getMappedCondition (AggregationOperationContext context ) {
@@ -703,6 +729,15 @@ public interface InputBuilder {
703
729
* @return
704
730
*/
705
731
AsBuilder filter (Field field );
732
+
733
+ /**
734
+ * Set the {@link AggregationExpression} resolving to an array to apply the {@code $filter} to.
735
+ *
736
+ * @param expression must not be {@literal null}.
737
+ * @return
738
+ * @since 3.4.13
739
+ */
740
+ AsBuilder filter (AggregationExpression expression );
706
741
}
707
742
708
743
/**
@@ -793,6 +828,14 @@ public AsBuilder filter(Field field) {
793
828
return this ;
794
829
}
795
830
831
+ @ Override
832
+ public AsBuilder filter (AggregationExpression expression ) {
833
+
834
+ Assert .notNull (expression , "Expression must not be null" );
835
+ filter .input = expression ;
836
+ return this ;
837
+ }
838
+
796
839
/*
797
840
* (non-Javadoc)
798
841
* @see org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.AsBuilder#as(java.lang.String)
@@ -1348,7 +1391,7 @@ public Reduce reduce(PropertyExpression... expressions) {
1348
1391
Assert .notNull (expressions , "PropertyExpressions must not be null" );
1349
1392
1350
1393
return new Reduce (Fields .field (fieldReference ), initialValue ,
1351
- Arrays .<AggregationExpression >asList (expressions ));
1394
+ Arrays .<AggregationExpression > asList (expressions ));
1352
1395
}
1353
1396
};
1354
1397
}
@@ -1708,7 +1751,7 @@ public Zip zip(Object... arrays) {
1708
1751
* @author Christoph Strobl
1709
1752
* @author Shashank Sharma
1710
1753
* @see <a href=
1711
- * "https://docs.mongodb.com/manual/reference/operator/aggregation/in/">https://docs.mongodb.com/manual/reference/operator/aggregation/in/</a>
1754
+ * "https://docs.mongodb.com/manual/reference/operator/aggregation/in/">https://docs.mongodb.com/manual/reference/operator/aggregation/in/</a>
1712
1755
* @since 2.2
1713
1756
*/
1714
1757
public static class In extends AbstractAggregationExpression {
@@ -1797,7 +1840,7 @@ public interface InBuilder {
1797
1840
*
1798
1841
* @author Christoph Strobl
1799
1842
* @see <a href=
1800
- * "https://docs.mongodb.com/manual/reference/operator/aggregation/arrayToObject/">https://docs.mongodb.com/manual/reference/operator/aggregation/arrayToObject/</a>
1843
+ * "https://docs.mongodb.com/manual/reference/operator/aggregation/arrayToObject/">https://docs.mongodb.com/manual/reference/operator/aggregation/arrayToObject/</a>
1801
1844
* @since 2.1
1802
1845
*/
1803
1846
public static class ArrayToObject extends AbstractAggregationExpression {
@@ -1845,7 +1888,7 @@ protected String getMongoMethod() {
1845
1888
return "$arrayToObject" ;
1846
1889
}
1847
1890
}
1848
-
1891
+
1849
1892
/**
1850
1893
* {@link AggregationExpression} for {@code $first} that returns the first element in an array. <br />
1851
1894
* <strong>NOTE:</strong> Requires MongoDB 4.4 or later.
@@ -1899,7 +1942,7 @@ protected String getMongoMethod() {
1899
1942
return "$first" ;
1900
1943
}
1901
1944
}
1902
-
1945
+
1903
1946
/**
1904
1947
* {@link AggregationExpression} for {@code $last} that returns the last element in an array. <br />
1905
1948
* <strong>NOTE:</strong> Requires MongoDB 4.4 or later.
0 commit comments