Skip to content

Commit 9014f77

Browse files
christophstroblmp911de
authored andcommitted
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.
1 parent f128e6d commit 9014f77

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core.query;
1717

18+
import java.util.Arrays;
1819
import java.util.HashMap;
1920
import java.util.Map;
2021
import java.util.Map.Entry;
@@ -192,7 +193,7 @@ public Field slice(String field, int size) {
192193
*/
193194
public Field slice(String field, int offset, int size) {
194195

195-
slices.put(field, new Integer[] { offset, size });
196+
slices.put(field, Arrays.asList(offset, size));
196197
return this;
197198
}
198199

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

+17
Original file line numberDiff line numberDiff line change
@@ -3768,6 +3768,23 @@ void shouldFindSubdocumentWithNullCorrectly() {
37683768
assertThat(loaded).isNotNull();
37693769
}
37703770

3771+
@Test // GH-3811
3772+
public void sliceShouldLimitCollectionValues() {
3773+
3774+
DocumentWithCollectionOfSimpleType source = new DocumentWithCollectionOfSimpleType();
3775+
source.id = "id-1";
3776+
source.values = Arrays.asList("spring", "data", "mongodb");
3777+
3778+
template.save(source);
3779+
3780+
Criteria criteria = Criteria.where("id").is(source.id);
3781+
Query query = Query.query(criteria);
3782+
query.fields().slice("values", 0, 1);
3783+
DocumentWithCollectionOfSimpleType target = template.findOne(query, DocumentWithCollectionOfSimpleType.class);
3784+
3785+
assertThat(target.values).containsExactly("spring");
3786+
}
3787+
37713788
private AtomicReference<ImmutableVersioned> createAfterSaveReference() {
37723789

37733790
AtomicReference<ImmutableVersioned> saved = new AtomicReference<>();

0 commit comments

Comments
 (0)