Skip to content

Commit ef13665

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-948 - Sort should be taken as is when no type information available.
Object type mapping for sort is skipped in the case no type information is present when executing query using mongo template.
1 parent 01cf9fb commit ef13665

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2179,7 +2179,8 @@ public DBCursor prepare(DBCursor cursor) {
21792179
cursorToUse = cursorToUse.limit(query.getLimit());
21802180
}
21812181
if (query.getSortObject() != null) {
2182-
cursorToUse = cursorToUse.sort(getMappedSortObject(query, type));
2182+
DBObject sortDbo = type != null ? getMappedSortObject(query, type) : query.getSortObject();
2183+
cursorToUse = cursorToUse.sort(sortDbo);
21832184
}
21842185
if (StringUtils.hasText(query.getHint())) {
21852186
cursorToUse = cursorToUse.hint(query.getHint());

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.dao.InvalidDataAccessApiUsageException;
4343
import org.springframework.data.annotation.Id;
4444
import org.springframework.data.annotation.Version;
45+
import org.springframework.data.domain.Sort;
4546
import org.springframework.data.mongodb.MongoDbFactory;
4647
import org.springframework.data.mongodb.core.convert.CustomConversions;
4748
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
@@ -50,11 +51,13 @@
5051
import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator;
5152
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
5253
import org.springframework.data.mongodb.core.query.BasicQuery;
54+
import org.springframework.data.mongodb.core.query.Criteria;
5355
import org.springframework.data.mongodb.core.query.Query;
5456
import org.springframework.data.mongodb.core.query.Update;
5557
import org.springframework.test.util.ReflectionTestUtils;
5658

5759
import com.mongodb.BasicDBObject;
60+
import com.mongodb.BasicDBObjectBuilder;
5861
import com.mongodb.DB;
5962
import com.mongodb.DBCollection;
6063
import com.mongodb.DBCursor;
@@ -329,6 +332,26 @@ public void findAllAndRemoveShouldNotTriggerRemoveIfFindResultIsEmpty() {
329332
verify(collection, never()).remove(Mockito.any(DBObject.class));
330333
}
331334

335+
/**
336+
* @see DATAMONGO-948
337+
*/
338+
@Test
339+
public void sortShouldBeTakenAsIsWhenExecutingQueryWithoutSpecificTypeInformation() {
340+
341+
Query query = Query.query(Criteria.where("foo").is("bar")).with(new Sort("foo"));
342+
template.executeQuery(query, "collection1", new DocumentCallbackHandler() {
343+
344+
@Override
345+
public void processDocument(DBObject dbObject) throws MongoException, DataAccessException {
346+
// nothing to do - just a test
347+
}
348+
});
349+
350+
ArgumentCaptor<DBObject> captor = ArgumentCaptor.forClass(DBObject.class);
351+
verify(cursor, times(1)).sort(captor.capture());
352+
assertThat(captor.getValue(), equalTo(new BasicDBObjectBuilder().add("foo", 1).get()));
353+
}
354+
332355
class AutogenerateableId {
333356

334357
@Id BigInteger id;

0 commit comments

Comments
 (0)