Skip to content

Commit f6f34c3

Browse files
christophstroblmp911de
authored andcommitted
Fix sort logging in MongoTemplate.
Original pull request: #4892 Closes #4860
1 parent c1e5dee commit f6f34c3

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private String asString() {
205205
* @author Christoph Strobl
206206
* @since 2.0
207207
*/
208-
static class DelegatingQueryCursorPreparer implements CursorPreparer {
208+
static class DelegatingQueryCursorPreparer implements SortingQueryCursorPreparer {
209209

210210
private final @Nullable CursorPreparer delegate;
211211
private Optional<Integer> limit = Optional.empty();
@@ -231,6 +231,11 @@ CursorPreparer limit(int limit) {
231231
public ReadPreference getReadPreference() {
232232
return delegate.getReadPreference();
233233
}
234+
235+
@Override
236+
public Document getSortObject() {
237+
return delegate instanceof SortingQueryCursorPreparer sqcp ? sqcp.getSortObject() : null;
238+
}
234239
}
235240

236241
/**

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

+15-4
Original file line numberDiff line numberDiff line change
@@ -2615,7 +2615,7 @@ protected <S, T> List<T> doFind(String collectionName,
26152615

26162616
if (LOGGER.isDebugEnabled()) {
26172617

2618-
Document mappedSort = getMappedSortObject(query, entityClass);
2618+
Document mappedSort = preparer instanceof SortingQueryCursorPreparer sqcp ? getMappedSortObject(sqcp.getSortObject(), entity) : null;
26192619
LOGGER.debug(String.format("find using query: %s fields: %s sort: %s for class: %s in collection: %s",
26202620
serializeToJsonSafely(mappedQuery), mappedFields, serializeToJsonSafely(mappedSort), entityClass,
26212621
collectionName));
@@ -2640,9 +2640,10 @@ <S, T> List<T> doFind(CollectionPreparer<MongoCollection<Document>> collectionPr
26402640
QueryContext queryContext = queryOperations.createQueryContext(new BasicQuery(query, fields));
26412641
Document mappedFields = queryContext.getMappedFields(entity, projection);
26422642
Document mappedQuery = queryContext.getMappedQuery(entity);
2643-
Document mappedSort = getMappedSortObject(query, sourceClass);
26442643

26452644
if (LOGGER.isDebugEnabled()) {
2645+
2646+
Document mappedSort = preparer instanceof SortingQueryCursorPreparer sqcp ? getMappedSortObject(sqcp.getSortObject(), sourceClass) : null;
26462647
LOGGER.debug(String.format("find using query: %s fields: %s sort: %s for class: %s in collection: %s",
26472648
serializeToJsonSafely(mappedQuery), mappedFields, serializeToJsonSafely(mappedSort), sourceClass,
26482649
collectionName));
@@ -3006,12 +3007,17 @@ private Document getMappedSortObject(@Nullable Query query, Class<?> type) {
30063007

30073008
@Nullable
30083009
private Document getMappedSortObject(Document sortObject, Class<?> type) {
3010+
return getMappedSortObject(sortObject, mappingContext.getPersistentEntity(type));
3011+
}
3012+
3013+
@Nullable
3014+
private Document getMappedSortObject(Document sortObject, MongoPersistentEntity<?> entity) {
30093015

30103016
if (ObjectUtils.isEmpty(sortObject)) {
30113017
return null;
30123018
}
30133019

3014-
return queryMapper.getMappedSort(sortObject, mappingContext.getPersistentEntity(type));
3020+
return queryMapper.getMappedSort(sortObject, entity);
30153021
}
30163022

30173023
/**
@@ -3364,7 +3370,7 @@ public T doWith(Document document) {
33643370
}
33653371
}
33663372

3367-
class QueryCursorPreparer implements CursorPreparer {
3373+
class QueryCursorPreparer implements SortingQueryCursorPreparer {
33683374

33693375
private final Query query;
33703376

@@ -3462,6 +3468,11 @@ public FindIterable<Document> prepare(FindIterable<Document> iterable) {
34623468
return cursorToUse;
34633469
}
34643470

3471+
@Nullable
3472+
@Override
3473+
public Document getSortObject() {
3474+
return sortObject;
3475+
}
34653476
}
34663477

34673478
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core;
17+
18+
import org.bson.Document;
19+
import org.springframework.lang.Nullable;
20+
21+
/**
22+
* @author Christoph Strobl
23+
*/
24+
interface SortingQueryCursorPreparer extends CursorPreparer {
25+
26+
@Nullable
27+
Document getSortObject();
28+
}

0 commit comments

Comments
 (0)