From e7d87219bdf96c073185265c1b425d9c76c3603b Mon Sep 17 00:00:00 2001 From: Ziemowit Stolarczyk Date: Wed, 27 Nov 2019 09:59:35 +0100 Subject: [PATCH] DATAMONGO-2428 - Add possibility to use Collection as parameter in and/or/nor operators --- .../data/mongodb/core/query/Criteria.java | 24 ++++++++- .../mongodb/core/query/CriteriaUnitTests.java | 50 +++++++++++++++++++ src/main/asciidoc/reference/mongodb.adoc | 3 ++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java index 7d57f16f54..c0c22ae8da 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java @@ -59,6 +59,7 @@ * @author Christoph Strobl * @author Mark Paluch * @author Andreas Zink + * @author Ziemowit Stolarczyk */ public class Criteria implements CriteriaDefinition { @@ -660,6 +661,13 @@ public BitwiseCriteriaOperators bits() { * @param criteria */ public Criteria orOperator(Criteria... criteria) { + return orOperator(Arrays.asList(criteria)); + } + + /** + * {@link #orOperator(Criteria...)} + */ + public Criteria orOperator(Collection criteria) { BasicDBList bsonList = createCriteriaList(criteria); return registerCriteriaChainElement(new Criteria("$or").is(bsonList)); } @@ -674,6 +682,13 @@ public Criteria orOperator(Criteria... criteria) { * @param criteria */ public Criteria norOperator(Criteria... criteria) { + return norOperator(Arrays.asList(criteria)); + } + + /** + * {@link #norOperator(Criteria...)} + */ + public Criteria norOperator(Collection criteria) { BasicDBList bsonList = createCriteriaList(criteria); return registerCriteriaChainElement(new Criteria("$nor").is(bsonList)); } @@ -688,6 +703,13 @@ public Criteria norOperator(Criteria... criteria) { * @param criteria */ public Criteria andOperator(Criteria... criteria) { + return andOperator(Arrays.asList(criteria)); + } + + /** + * {@link #andOperator(Criteria...)} + */ + public Criteria andOperator(Collection criteria) { BasicDBList bsonList = createCriteriaList(criteria); return registerCriteriaChainElement(new Criteria("$and").is(bsonList)); } @@ -781,7 +803,7 @@ protected Document getSingleCriteriaObject() { return queryCriteria; } - private BasicDBList createCriteriaList(Criteria[] criteria) { + private BasicDBList createCriteriaList(Collection criteria) { BasicDBList bsonList = new BasicDBList(); for (Criteria c : criteria) { bsonList.add(c.getCriteriaObject()); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/CriteriaUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/CriteriaUnitTests.java index 13af8b0449..120207ca51 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/CriteriaUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/CriteriaUnitTests.java @@ -18,6 +18,7 @@ import static org.springframework.data.mongodb.test.util.Assertions.*; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import org.bson.Document; @@ -34,6 +35,7 @@ * @author Thomas Darimont * @author Christoph Strobl * @author Andreas Zink + * @author Ziemowit Stolarczyk */ public class CriteriaUnitTests { @@ -80,6 +82,54 @@ public void equalIfCriteriaMatches() { assertThat(right).isNotEqualTo(left); } + @Test + public void shouldBuildCorrectAndOperator() { + //given + Collection operatorCriteria = Arrays.asList(Criteria.where("x").is(true), + Criteria.where("y").is(42), + Criteria.where("z").is("value")); + Document expectedResult = Document + .parse("{\"$and\":[{\"x\":true}, {\"y\":42}, {\"z\":\"value\"}], \"foo\":\"bar\"}"); + + //when + Criteria criteria = Criteria.where("foo").is("bar").andOperator(operatorCriteria); + + //then + assertThat(criteria.getCriteriaObject()).isEqualTo(expectedResult); + } + + @Test + public void shouldBuildCorrectOrOperator() { + //given + Collection operatorCriteria = Arrays.asList(Criteria.where("x").is(true), + Criteria.where("y").is(42), + Criteria.where("z").is("value")); + Document expectedResult = Document + .parse("{\"$or\":[{\"x\":true}, {\"y\":42}, {\"z\":\"value\"}], \"foo\":\"bar\"}"); + + //when + Criteria criteria = Criteria.where("foo").is("bar").orOperator(operatorCriteria); + + //then + assertThat(criteria.getCriteriaObject()).isEqualTo(expectedResult); + } + + @Test + public void shouldBuildCorrectNorOperator() { + //given + Collection operatorCriteria = Arrays.asList(Criteria.where("x").is(true), + Criteria.where("y").is(42), + Criteria.where("z").is("value")); + Document expectedResult = Document + .parse("{\"$nor\":[{\"x\":true}, {\"y\":42}, {\"z\":\"value\"}], \"foo\":\"bar\"}"); + + //when + Criteria criteria = Criteria.where("foo").is("bar").norOperator(operatorCriteria); + + //then + assertThat(criteria.getCriteriaObject()).isEqualTo(expectedResult); + } + @Test // DATAMONGO-507 public void shouldThrowExceptionWhenTryingToNegateAndOperation() { assertThatIllegalArgumentException().isThrownBy(() -> new Criteria() // diff --git a/src/main/asciidoc/reference/mongodb.adoc b/src/main/asciidoc/reference/mongodb.adoc index fe5d0f8ffd..2c3231f6b6 100644 --- a/src/main/asciidoc/reference/mongodb.adoc +++ b/src/main/asciidoc/reference/mongodb.adoc @@ -1193,6 +1193,7 @@ The `Criteria` class provides the following methods, all of which correspond to * `Criteria` *all* `(Object o)` Creates a criterion using the `$all` operator * `Criteria` *and* `(String key)` Adds a chained `Criteria` with the specified `key` to the current `Criteria` and returns the newly created one * `Criteria` *andOperator* `(Criteria... criteria)` Creates an and query using the `$and` operator for all of the provided criteria (requires MongoDB 2.0 or later) +* `Criteria` *andOperator* `(Collection criteria)` Creates an and query using the `$and` operator for all of the provided criteria (requires MongoDB 2.0 or later) * `Criteria` *elemMatch* `(Criteria c)` Creates a criterion using the `$elemMatch` operator * `Criteria` *exists* `(boolean b)` Creates a criterion using the `$exists` operator * `Criteria` *gt* `(Object o)` Creates a criterion using the `$gt` operator @@ -1206,8 +1207,10 @@ The `Criteria` class provides the following methods, all of which correspond to * `Criteria` *ne* `(Object o)` Creates a criterion using the `$ne` operator * `Criteria` *nin* `(Object... o)` Creates a criterion using the `$nin` operator * `Criteria` *norOperator* `(Criteria... criteria)` Creates an nor query using the `$nor` operator for all of the provided criteria +* `Criteria` *norOperator* `(Collection criteria)` Creates an nor query using the `$nor` operator for all of the provided criteria * `Criteria` *not* `()` Creates a criterion using the `$not` meta operator which affects the clause directly following * `Criteria` *orOperator* `(Criteria... criteria)` Creates an or query using the `$or` operator for all of the provided criteria +* `Criteria` *orOperator* `(Collection criteria)` Creates an or query using the `$or` operator for all of the provided criteria * `Criteria` *regex* `(String re)` Creates a criterion using a `$regex` * `Criteria` *size* `(int s)` Creates a criterion using the `$size` operator * `Criteria` *type* `(int t)` Creates a criterion using the `$type` operator