Skip to content

Commit f01b1bc

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

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
@@ -2596,7 +2596,7 @@ protected <S, T> List<T> doFind(String collectionName,
25962596

25972597
if (LOGGER.isDebugEnabled()) {
25982598

2599-
Document mappedSort = getMappedSortObject(query, entityClass);
2599+
Document mappedSort = preparer instanceof SortingQueryCursorPreparer sqcp ? getMappedSortObject(sqcp.getSortObject(), entity) : null;
26002600
LOGGER.debug(String.format("find using query: %s fields: %s sort: %s for class: %s in collection: %s",
26012601
serializeToJsonSafely(mappedQuery), mappedFields, serializeToJsonSafely(mappedSort), entityClass,
26022602
collectionName));
@@ -2621,9 +2621,10 @@ <S, T> List<T> doFind(CollectionPreparer<MongoCollection<Document>> collectionPr
26212621
QueryContext queryContext = queryOperations.createQueryContext(new BasicQuery(query, fields));
26222622
Document mappedFields = queryContext.getMappedFields(entity, projection);
26232623
Document mappedQuery = queryContext.getMappedQuery(entity);
2624-
Document mappedSort = getMappedSortObject(query, sourceClass);
26252624

26262625
if (LOGGER.isDebugEnabled()) {
2626+
2627+
Document mappedSort = preparer instanceof SortingQueryCursorPreparer sqcp ? getMappedSortObject(sqcp.getSortObject(), sourceClass) : null;
26272628
LOGGER.debug(String.format("find using query: %s fields: %s sort: %s for class: %s in collection: %s",
26282629
serializeToJsonSafely(mappedQuery), mappedFields, serializeToJsonSafely(mappedSort), sourceClass,
26292630
collectionName));
@@ -2988,12 +2989,17 @@ private Document getMappedSortObject(@Nullable Query query, Class<?> type) {
29882989

29892990
@Nullable
29902991
private Document getMappedSortObject(Document sortObject, Class<?> type) {
2992+
return getMappedSortObject(sortObject, mappingContext.getPersistentEntity(type));
2993+
}
2994+
2995+
@Nullable
2996+
private Document getMappedSortObject(Document sortObject, MongoPersistentEntity<?> entity) {
29912997

29922998
if (ObjectUtils.isEmpty(sortObject)) {
29932999
return null;
29943000
}
29953001

2996-
return queryMapper.getMappedSort(sortObject, mappingContext.getPersistentEntity(type));
3002+
return queryMapper.getMappedSort(sortObject, entity);
29973003
}
29983004

29993005
/**
@@ -3346,7 +3352,7 @@ public T doWith(Document document) {
33463352
}
33473353
}
33483354

3349-
class QueryCursorPreparer implements CursorPreparer {
3355+
class QueryCursorPreparer implements SortingQueryCursorPreparer {
33503356

33513357
private final Query query;
33523358

@@ -3444,6 +3450,11 @@ public FindIterable<Document> prepare(FindIterable<Document> iterable) {
34443450
return cursorToUse;
34453451
}
34463452

3453+
@Nullable
3454+
@Override
3455+
public Document getSortObject() {
3456+
return sortObject;
3457+
}
34473458
}
34483459

34493460
/**
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)