Skip to content

Fix sort logging in MongoTemplate #4892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.5.0-SNAPSHOT</version>
<version>4.5.x-GH-4860-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.5.0-SNAPSHOT</version>
<version>4.5.x-GH-4860-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.5.0-SNAPSHOT</version>
<version>4.5.x-GH-4860-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Original file line number Diff line number Diff line change
@@ -205,7 +205,7 @@ private String asString() {
* @author Christoph Strobl
* @since 2.0
*/
static class DelegatingQueryCursorPreparer implements CursorPreparer {
static class DelegatingQueryCursorPreparer implements SortingQueryCursorPreparer {

private final @Nullable CursorPreparer delegate;
private Optional<Integer> limit = Optional.empty();
@@ -231,6 +231,11 @@ CursorPreparer limit(int limit) {
public ReadPreference getReadPreference() {
return delegate.getReadPreference();
}

@Override
public Document getSortObject() {
return delegate instanceof SortingQueryCursorPreparer sqcp ? sqcp.getSortObject() : null;
}
}

/**
Original file line number Diff line number Diff line change
@@ -2596,7 +2596,7 @@ protected <S, T> List<T> doFind(String collectionName,

if (LOGGER.isDebugEnabled()) {

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

if (LOGGER.isDebugEnabled()) {

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

@Nullable
private Document getMappedSortObject(Document sortObject, Class<?> type) {
return getMappedSortObject(sortObject, mappingContext.getPersistentEntity(type));
}

@Nullable
private Document getMappedSortObject(Document sortObject, MongoPersistentEntity<?> entity) {

if (ObjectUtils.isEmpty(sortObject)) {
return null;
}

return queryMapper.getMappedSort(sortObject, mappingContext.getPersistentEntity(type));
return queryMapper.getMappedSort(sortObject, entity);
}

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

class QueryCursorPreparer implements CursorPreparer {
class QueryCursorPreparer implements SortingQueryCursorPreparer {

private final Query query;

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

@Nullable
@Override
public Document getSortObject() {
return sortObject;
}
}

/**
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core;

import org.bson.Document;
import org.springframework.lang.Nullable;

/**
* @author Christoph Strobl
*/
interface SortingQueryCursorPreparer extends CursorPreparer {

@Nullable
Document getSortObject();
}