Browse Source

Fix slice argument in query fields projection.

We now use a Collection instead of an Array to pass on $slice projection values for offset and limit.

Closes: #3811
Original pull request: #3812.
3.1.x
Christoph Strobl 4 years ago committed by Mark Paluch
parent
commit
701153ac8f
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java
  2. 17
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

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

@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
*/
package org.springframework.data.mongodb.core.query;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
@ -136,7 +137,7 @@ public class Field { @@ -136,7 +137,7 @@ public class Field {
*/
public Field slice(String field, int offset, int size) {
slices.put(field, new Integer[] { offset, size });
slices.put(field, Arrays.asList(offset, size));
return this;
}

17
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

@ -3703,6 +3703,23 @@ public class MongoTemplateTests { @@ -3703,6 +3703,23 @@ public class MongoTemplateTests {
assertThat(template.find(new BasicQuery("{}").with(Sort.by("id")), WithIdAndFieldAnnotation.class)).isNotEmpty();
}
@Test // GH-3811
public void sliceShouldLimitCollectionValues() {
DocumentWithCollectionOfSimpleType source = new DocumentWithCollectionOfSimpleType();
source.id = "id-1";
source.values = Arrays.asList("spring", "data", "mongodb");
template.save(source);
Criteria criteria = Criteria.where("id").is(source.id);
Query query = Query.query(criteria);
query.fields().slice("values", 0, 1);
DocumentWithCollectionOfSimpleType target = template.findOne(query, DocumentWithCollectionOfSimpleType.class);
assertThat(target.values).containsExactly("spring");
}
private AtomicReference<ImmutableVersioned> createAfterSaveReference() {
AtomicReference<ImmutableVersioned> saved = new AtomicReference<>();

Loading…
Cancel
Save