DATAMONGO-2287 - Add support for $in aggregation pipeline operator.
Original pull request: #760.
This commit is contained in:
committed by
Mark Paluch
parent
bb280bd59b
commit
a3ef9b5856
@@ -33,6 +33,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @author Shashank Sharma
|
||||
* @since 1.0
|
||||
*/
|
||||
public class ArrayOperators {
|
||||
@@ -1442,6 +1443,7 @@ public class ArrayOperators {
|
||||
* {@link AggregationExpression} for {@code $in}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Shashank Sharma
|
||||
*/
|
||||
public static class In extends AbstractAggregationExpression {
|
||||
|
||||
@@ -1496,6 +1498,28 @@ public class ArrayOperators {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Support for Aggregation In Search an Element in List of Objects to Filter Start creating {@link In}.
|
||||
*
|
||||
* @author Shashank Sharma
|
||||
* @param elementList must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static InBuilder arrayOf(final List<Object> elementList) {
|
||||
|
||||
Assert.notNull(elementList, "Elements must not be null!");
|
||||
|
||||
return new InBuilder() {
|
||||
|
||||
@Override
|
||||
public In containsValue(Object value) {
|
||||
|
||||
Assert.notNull(value, "Value must not be null!");
|
||||
return new In(Arrays.asList(value, elementList));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.springframework.data.mongodb.core.aggregation.ArrayOperators.ArrayToO
|
||||
* Unit tests for {@link ArrayOperators}
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Shashank Sharma
|
||||
* @currentRead Royal Assassin - Robin Hobb
|
||||
*/
|
||||
public class ArrayOperatorsUnitTests {
|
||||
@@ -61,4 +62,12 @@ public class ArrayOperatorsUnitTests {
|
||||
assertThat(ArrayToObject.arrayToObject(source).toDocument(Aggregation.DEFAULT_CONTEXT))
|
||||
.isEqualTo(Document.parse("{ $arrayToObject: [ [ \"king\", \"shrewd\"], [ \"prince\", \"verity\" ] ] } "));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2287
|
||||
public void inArrayAggregationWithArgumentList() {
|
||||
|
||||
assertThat(ArrayOperators.In.arrayOf(Arrays.asList("Shashank", "Sharma")).containsValue("$userName")
|
||||
.toDocument(Aggregation.DEFAULT_CONTEXT))
|
||||
.isEqualTo(Document.parse("{ \"$in\" : [\"$userName\", [\"Shashank\", \"Sharma\"]] }"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user