'Spring MongoDB query sorting

I'm fairly new on mongodb, and while I'm trying to make ordered mongodb query. But spring data mongodb's sort method is deprecated. So I used org.springframework.data.domain.Sort:

Query query = new Query();
query.with(new Sort(Sort.Direction.ASC,"pdate"));
return mongoTemplate.find(query, Product.class);

I used this code block. But its not sorting the data. So can you prefer to use any useful method for this practice?



Solution 1:[1]

You can define your sort in this manner to ignore case:

new Sort(new Order(Direction.ASC, FIELD_NAME).ignoreCase()

Solution 2:[2]

NEW ANSWER - Spring Data Moore

Use Sort.by

Query().addCriteria(Criteria.where("field").`is`(value)).with(Sort.by(Sort.Direction.DESC, "sortField"))

Solution 3:[3]

sWhen you've written a custom query in your repository then you can perform sorting during invocation. Like,

Repository

@Query("{ 'id' : ?0}")
List<Student> findStudent(String id, Sort sort);

During invocation

Sort sort = new Sort(Sort.Direction.ASC, "date")
List<Student> students = studentRepo.findStudent(1, sort);  

I hope this helps! :)

Solution 4:[4]

query.with(new Sort(Sort.Direction.ASC, "timestamp"));

remember sort parameter as field, 1 or -1 to specify an ascending or descending sort respectively.

Solution 5:[5]

This is how you sort a field value by Descending order. My field value is "nominationTimestamp", but for you it could be "firstName" for example.

List<Movie> result = myMovieRepository.findAll(Sort.by(Sort.Direction.DESC, "nominationTimestamp")); 

myMovieRepository is an instance of whatever class extends MongoRepository<>.

Solution 6:[6]

You can use aggregation for sorting your data. You have to use matching and grouping criteria for aggregation query and unwind your field.

AggregationOperation match = Aggregation.match(matching criteria);
AggregationOperation group = Aggregation.group("fieldname");
AggregationOperation sort = Aggregation.sort(Sort.Direction.ASC, "fieldname");
Aggregation aggregation = Aggregation.newAggregation(Aggregation.unwind("fieldname"),match,group,sort);

Solution 7:[7]

This one worked for me:

query.with(Sort.by(Sort.Order.asc("pdate")));

Solution 8:[8]

I'm using TypedAggregation with mongoTemplate in spring data to sort and limit the results set.

import static org.springframework.data.mongodb.core.aggregation.Aggregation.limit;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.sort;
import org.springframework.data.domain.Sort.Direction;

TypedAggregation<ObjectType> agg = newAggregation(ObjectType.class,
    match(matching Criteria),
    project("_id", ...),
    sort(Direction.ASC, "_id"),
    limit(pageSize));

List<RESULT_OBJECT> mappedResult = mongoTemplate.aggregate(agg, COLLECTION_NAME, RESULT_OBJECT.class).getMappedResults();

Solution 9:[9]

spring-data-commons version 2.3.5

The Sort constructor is private, so:

Query query = new Query();
query.with(Sort.by(Sort.Direction.ASC,"pdate"));
return mongoTemplate.find(query, Product.class);

Solution 10:[10]

You can use Aggregation in repository

Repository

  @Aggregation(pipeline ={
    "{$match: { id : ?0 }",
    "{$sort: {date: 1}}",
    "{$limit: 1}"
  }
  Optional<Student> findStudent(String id);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dev
Solution 2 Ronny Shibley
Solution 3
Solution 4 Dharman
Solution 5
Solution 6 Jijesh Kumar
Solution 7 Tomerikoo
Solution 8
Solution 9 Leonel Sanches da Silva
Solution 10