Skip to content

Commit 0b27635

Browse files
Ziemowitmp911de
authored andcommitted
Add possibility to use Collection<Criteria> as parameter in and/or/nor operators.
Closes #3286. Original pull request: #811.
1 parent 198ebaa commit 0b27635

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
* @author Christoph Strobl
5959
* @author Mark Paluch
6060
* @author Andreas Zink
61+
* @author Ziemowit Stolarczyk
6162
*/
6263
public class Criteria implements CriteriaDefinition {
6364

@@ -678,6 +679,13 @@ public BitwiseCriteriaOperators bits() {
678679
* @return this.
679680
*/
680681
public Criteria orOperator(Criteria... criteria) {
682+
return orOperator(Arrays.asList(criteria));
683+
}
684+
685+
/**
686+
* {@link #orOperator(Criteria...)}
687+
*/
688+
public Criteria orOperator(Collection<Criteria> criteria) {
681689
BasicDBList bsonList = createCriteriaList(criteria);
682690
return registerCriteriaChainElement(new Criteria("$or").is(bsonList));
683691
}
@@ -693,6 +701,13 @@ public Criteria orOperator(Criteria... criteria) {
693701
* @return this.
694702
*/
695703
public Criteria norOperator(Criteria... criteria) {
704+
return norOperator(Arrays.asList(criteria));
705+
}
706+
707+
/**
708+
* {@link #norOperator(Criteria...)}
709+
*/
710+
public Criteria norOperator(Collection<Criteria> criteria) {
696711
BasicDBList bsonList = createCriteriaList(criteria);
697712
return registerCriteriaChainElement(new Criteria("$nor").is(bsonList));
698713
}
@@ -708,6 +723,13 @@ public Criteria norOperator(Criteria... criteria) {
708723
* @return this.
709724
*/
710725
public Criteria andOperator(Criteria... criteria) {
726+
return andOperator(Arrays.asList(criteria));
727+
}
728+
729+
/**
730+
* {@link #andOperator(Criteria...)}
731+
*/
732+
public Criteria andOperator(Collection<Criteria> criteria) {
711733
BasicDBList bsonList = createCriteriaList(criteria);
712734
return registerCriteriaChainElement(new Criteria("$and").is(bsonList));
713735
}
@@ -801,7 +823,7 @@ protected Document getSingleCriteriaObject() {
801823
return queryCriteria;
802824
}
803825

804-
private BasicDBList createCriteriaList(Criteria[] criteria) {
826+
private BasicDBList createCriteriaList(Collection<Criteria> criteria) {
805827
BasicDBList bsonList = new BasicDBList();
806828
for (Criteria c : criteria) {
807829
bsonList.add(c.getCriteriaObject());

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/CriteriaUnitTests.java

+50
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.springframework.data.mongodb.test.util.Assertions.*;
1919

2020
import java.util.Arrays;
21+
import java.util.Collection;
2122
import java.util.Collections;
2223

2324
import org.bson.Document;
@@ -34,6 +35,7 @@
3435
* @author Thomas Darimont
3536
* @author Christoph Strobl
3637
* @author Andreas Zink
38+
* @author Ziemowit Stolarczyk
3739
*/
3840
public class CriteriaUnitTests {
3941

@@ -80,6 +82,54 @@ public void equalIfCriteriaMatches() {
8082
assertThat(right).isNotEqualTo(left);
8183
}
8284

85+
@Test
86+
public void shouldBuildCorrectAndOperator() {
87+
//given
88+
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true),
89+
Criteria.where("y").is(42),
90+
Criteria.where("z").is("value"));
91+
Document expectedResult = Document
92+
.parse("{\"$and\":[{\"x\":true}, {\"y\":42}, {\"z\":\"value\"}], \"foo\":\"bar\"}");
93+
94+
//when
95+
Criteria criteria = Criteria.where("foo").is("bar").andOperator(operatorCriteria);
96+
97+
//then
98+
assertThat(criteria.getCriteriaObject()).isEqualTo(expectedResult);
99+
}
100+
101+
@Test
102+
public void shouldBuildCorrectOrOperator() {
103+
//given
104+
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true),
105+
Criteria.where("y").is(42),
106+
Criteria.where("z").is("value"));
107+
Document expectedResult = Document
108+
.parse("{\"$or\":[{\"x\":true}, {\"y\":42}, {\"z\":\"value\"}], \"foo\":\"bar\"}");
109+
110+
//when
111+
Criteria criteria = Criteria.where("foo").is("bar").orOperator(operatorCriteria);
112+
113+
//then
114+
assertThat(criteria.getCriteriaObject()).isEqualTo(expectedResult);
115+
}
116+
117+
@Test
118+
public void shouldBuildCorrectNorOperator() {
119+
//given
120+
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true),
121+
Criteria.where("y").is(42),
122+
Criteria.where("z").is("value"));
123+
Document expectedResult = Document
124+
.parse("{\"$nor\":[{\"x\":true}, {\"y\":42}, {\"z\":\"value\"}], \"foo\":\"bar\"}");
125+
126+
//when
127+
Criteria criteria = Criteria.where("foo").is("bar").norOperator(operatorCriteria);
128+
129+
//then
130+
assertThat(criteria.getCriteriaObject()).isEqualTo(expectedResult);
131+
}
132+
83133
@Test // DATAMONGO-507
84134
public void shouldThrowExceptionWhenTryingToNegateAndOperation() {
85135
assertThatIllegalArgumentException().isThrownBy(() -> new Criteria() //

src/main/asciidoc/reference/mongodb.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,7 @@ The `Criteria` class provides the following methods, all of which correspond to
12001200
* `Criteria` *all* `(Object o)` Creates a criterion using the `$all` operator
12011201
* `Criteria` *and* `(String key)` Adds a chained `Criteria` with the specified `key` to the current `Criteria` and returns the newly created one
12021202
* `Criteria` *andOperator* `(Criteria... criteria)` Creates an and query using the `$and` operator for all of the provided criteria (requires MongoDB 2.0 or later)
1203+
* `Criteria` *andOperator* `(Collection<Criteria> criteria)` Creates an and query using the `$and` operator for all of the provided criteria (requires MongoDB 2.0 or later)
12031204
* `Criteria` *elemMatch* `(Criteria c)` Creates a criterion using the `$elemMatch` operator
12041205
* `Criteria` *exists* `(boolean b)` Creates a criterion using the `$exists` operator
12051206
* `Criteria` *gt* `(Object o)` Creates a criterion using the `$gt` operator
@@ -1213,8 +1214,10 @@ The `Criteria` class provides the following methods, all of which correspond to
12131214
* `Criteria` *ne* `(Object o)` Creates a criterion using the `$ne` operator
12141215
* `Criteria` *nin* `(Object... o)` Creates a criterion using the `$nin` operator
12151216
* `Criteria` *norOperator* `(Criteria... criteria)` Creates an nor query using the `$nor` operator for all of the provided criteria
1217+
* `Criteria` *norOperator* `(Collection<Criteria> criteria)` Creates an nor query using the `$nor` operator for all of the provided criteria
12161218
* `Criteria` *not* `()` Creates a criterion using the `$not` meta operator which affects the clause directly following
12171219
* `Criteria` *orOperator* `(Criteria... criteria)` Creates an or query using the `$or` operator for all of the provided criteria
1220+
* `Criteria` *orOperator* `(Collection<Criteria> criteria)` Creates an or query using the `$or` operator for all of the provided criteria
12181221
* `Criteria` *regex* `(String re)` Creates a criterion using a `$regex`
12191222
* `Criteria` *size* `(int s)` Creates a criterion using the `$size` operator
12201223
* `Criteria` *type* `(int t)` Creates a criterion using the `$type` operator

0 commit comments

Comments
 (0)