DATADOC-46 - Added implementations for 'In' and 'NotIn'.
Added keyword interpretation as well as examples and test cases. Fixed bug in Criteria where a typo caused an invalid operator being used for not-in ($min -> $nin).
This commit is contained in:
@@ -123,7 +123,7 @@ public class Criteria implements CriteriaDefinition {
|
||||
* @return
|
||||
*/
|
||||
public Criteria nin(Object... o) {
|
||||
criteria.put("$min", o);
|
||||
criteria.put("$nin", o);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.document.mongodb.repository;
|
||||
|
||||
import static org.springframework.data.document.mongodb.query.Criteria.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -141,6 +142,10 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Criteria> {
|
||||
return criteria.not().is(null);
|
||||
case IS_NULL:
|
||||
return criteria.is(null);
|
||||
case NOT_IN:
|
||||
return criteria.nin(nextAsArray(parameters));
|
||||
case IN:
|
||||
return criteria.in(nextAsArray(parameters));
|
||||
case LIKE:
|
||||
String value = parameters.next().toString();
|
||||
return criteria.is(toLikeRegex(value));
|
||||
@@ -154,6 +159,17 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Criteria> {
|
||||
}
|
||||
|
||||
|
||||
private Object[] nextAsArray(Iterator<Object> iterator) {
|
||||
Object next = iterator.next();
|
||||
|
||||
if (next instanceof Collection) {
|
||||
return ((Collection<?>) next).toArray();
|
||||
} else if (next.getClass().isArray()) {
|
||||
return (Object[]) next;
|
||||
}
|
||||
|
||||
return new Object[] { next };
|
||||
}
|
||||
|
||||
private Pattern toLikeRegex(String source) {
|
||||
|
||||
|
||||
@@ -162,4 +162,21 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
assertThat(result.size(), is(1));
|
||||
assertThat(result, hasItem(dave));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void findsPeopleByFirstnameInVarargs() {
|
||||
|
||||
List<Person> result = repository.findByFirstnameIn("Dave", "Carter");
|
||||
assertThat(result.size(), is(2));
|
||||
assertThat(result, hasItems(dave, carter));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsPeopleByFirstnameNotInCollection() {
|
||||
|
||||
List<Person> result = repository.findByFirstnameNotIn(Arrays.asList("Boyd", "Carter"));
|
||||
assertThat(result.size(), is(3));
|
||||
assertThat(result, hasItems(dave, leroi, stefan));
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.document.mongodb.repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -67,6 +68,21 @@ public interface PersonRepository extends MongoRepository<Person, String> {
|
||||
*/
|
||||
Page<Person> findByLastnameLike(String lastname, Pageable pageable);
|
||||
|
||||
/**
|
||||
* Returns all {@link Person}s with a firstname contained in the given varargs.
|
||||
*
|
||||
* @param firstnames
|
||||
* @return
|
||||
*/
|
||||
List<Person> findByFirstnameIn(String... firstnames);
|
||||
|
||||
/**
|
||||
* Returns all {@link Person}s with a firstname not contained in the given collection.
|
||||
*
|
||||
* @param firstnames
|
||||
* @return
|
||||
*/
|
||||
List<Person> findByFirstnameNotIn(Collection<String> firstnames);
|
||||
|
||||
/**
|
||||
* Returns all {@link Person}s with an age between the two given values.
|
||||
|
||||
@@ -6,6 +6,7 @@ Changes in version 1.0.0.M2 MongoDB
|
||||
|
||||
Repository
|
||||
* Adapted new metamodel API (DATADOC-47, DATACMNS-17)
|
||||
* Added support for 'In' and 'NotIn' keyword (DATADOC-46)
|
||||
|
||||
Changes in version 1.0.0.M1 MongoDB (2011-02-14)
|
||||
------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user