Skip to content

DATAMONGO-2428 - Add possibility to use Collection<Criteria> as parameter in and/or/nor operators #811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author Andreas Zink
* @author Ziemowit Stolarczyk
*/
public class Criteria implements CriteriaDefinition {

Expand Down Expand Up @@ -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> criteria) {
BasicDBList bsonList = createCriteriaList(criteria);
return registerCriteriaChainElement(new Criteria("$or").is(bsonList));
}
Expand All @@ -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> criteria) {
BasicDBList bsonList = createCriteriaList(criteria);
return registerCriteriaChainElement(new Criteria("$nor").is(bsonList));
}
Expand All @@ -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> criteria) {
BasicDBList bsonList = createCriteriaList(criteria);
return registerCriteriaChainElement(new Criteria("$and").is(bsonList));
}
Expand Down Expand Up @@ -781,7 +803,7 @@ protected Document getSingleCriteriaObject() {
return queryCriteria;
}

private BasicDBList createCriteriaList(Criteria[] criteria) {
private BasicDBList createCriteriaList(Collection<Criteria> criteria) {
BasicDBList bsonList = new BasicDBList();
for (Criteria c : criteria) {
bsonList.add(c.getCriteriaObject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,6 +35,7 @@
* @author Thomas Darimont
* @author Christoph Strobl
* @author Andreas Zink
* @author Ziemowit Stolarczyk
*/
public class CriteriaUnitTests {

Expand Down Expand Up @@ -80,6 +82,54 @@ public void equalIfCriteriaMatches() {
assertThat(right).isNotEqualTo(left);
}

@Test
public void shouldBuildCorrectAndOperator() {
//given
Collection<Criteria> 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<Criteria> 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<Criteria> 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() //
Expand Down
3 changes: 3 additions & 0 deletions src/main/asciidoc/reference/mongodb.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,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> 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
Expand All @@ -1198,8 +1199,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> 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> 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
Expand Down