Skip to content

Commit

Permalink
Fix slice argument in query fields projection.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
christophstrobl authored and mp911de committed Sep 8, 2021
1 parent 6b394e4 commit 701153a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -136,7 +137,7 @@ public Field slice(String field, int size) {
*/
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3703,6 +3703,23 @@ public void sortOnIdFieldWithExplicitTypeShouldWork() {
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<>();
Expand Down

0 comments on commit 701153a

Please sign in to comment.