diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Criteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Criteria.java index 3ab683bad..a4bbb68aa 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Criteria.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Criteria.java @@ -16,6 +16,7 @@ package org.springframework.data.document.mongodb.query; import java.util.ArrayList; +import java.util.Collection; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; @@ -156,14 +157,29 @@ public class Criteria implements CriteriaDefinition { /** * Creates a criterion using the $in operator * - * @param o + * @param o the values to match against * @return */ public Criteria in(Object... o) { + if (o.length > 1 && o[1] instanceof Collection) { + throw new InvalidDocumentStoreApiUsageException("You can only pass in one argument of type " + o[1].getClass().getName()); + } criteria.put("$in", o); return this; } + /** + * Creates a criterion using the $in operator + * + * @param c the collection containing the values to match against + * @return + */ + public Criteria in(Collection c) { + System.out.println(c.getClass()); + criteria.put("$in", c.toArray()); + return this; + } + /** * Creates a criterion using the $nin operator * diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java index c351a0852..af6b1cacb 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoTemplateTests.java @@ -24,11 +24,13 @@ import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; import static org.springframework.data.document.mongodb.query.Criteria.where; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import org.bson.types.ObjectId; +import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -36,6 +38,7 @@ import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.data.document.InvalidDocumentStoreApiUsageException; import org.springframework.data.document.mongodb.convert.MappingMongoConverter; import org.springframework.data.document.mongodb.convert.MongoConverter; import org.springframework.data.document.mongodb.mapping.MongoMappingContext; @@ -322,6 +325,47 @@ public class MongoTemplateTests { assertThat(results3.size(), is(1)); } + @Test + public void testUsingInQueryWithList() throws Exception { + + template.remove(new Query(), PersonWithIdPropertyOfTypeObjectId.class); + + PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId(); + p1.setFirstName("Sven"); + p1.setAge(11); + template.insert(p1); + PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId(); + p2.setFirstName("Mary"); + p2.setAge(21); + template.insert(p2); + PersonWithIdPropertyOfTypeObjectId p3 = new PersonWithIdPropertyOfTypeObjectId(); + p3.setFirstName("Ann"); + p3.setAge(31); + template.insert(p3); + PersonWithIdPropertyOfTypeObjectId p4 = new PersonWithIdPropertyOfTypeObjectId(); + p4.setFirstName("John"); + p4.setAge(41); + template.insert(p4); + + List l1 = new ArrayList(); + l1.add(11); + l1.add(21); + l1.add(41); + Query q1 = new Query(Criteria.where("age").in(l1)); + List results1 = template.find(q1, PersonWithIdPropertyOfTypeObjectId.class); + Query q2 = new Query(Criteria.where("age").in(l1.toArray())); + List results2 = template.find(q2, PersonWithIdPropertyOfTypeObjectId.class); + assertThat(results1.size(), is(3)); + assertThat(results2.size(), is(3)); + try { + List l2 = new ArrayList(); + l2.add(31); + Query q3 = new Query(Criteria.where("age").in(l1, l2)); + template.find(q3, PersonWithIdPropertyOfTypeObjectId.class); + Assert.fail("Should have trown an InvalidDocumentStoreApiUsageException"); + } catch (InvalidDocumentStoreApiUsageException e) {} + } + @Test public void testUsingAnOrQuery() throws Exception {