Java Spring MongoDB 查询排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18521009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:44:14  来源:igfitidea点击:

Spring MongoDB query sorting

javaspringmongodbsorting

提问by ?lker Korkut

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:

我对 mongodb 还很陌生,虽然我正在尝试进行有序的 mongodb 查询。但是spring data mongodb的sort方法被弃用了。所以我用了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?

我使用了这个代码块。但它没有对数据进行排序。那么您是否更愿意使用任何有用的方法来进行这种练习?

采纳答案by dev

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

您可以以这种方式定义排序以忽略大小写:

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

回答by Jijesh Kumar

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);

回答by imbond

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

s当您在存储库中编写了自定义查询后,您就可以在调用期间执行排序。喜欢,

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! :)

我希望这有帮助!:)

回答by Pravin

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

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

记住 sort 参数作为字段,1 或 -1 分别指定升序或降序排序。

回答by Ronny Shibley

NEW ANSWER- Spring Data Moore

新答案- Spring Data Moore

Use Sort.by

Sort.by

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

回答by Hany Sakr

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

我在 spring 数据中使用 TypedAggregation 和 mongoTemplate 来排序和限制结果集。

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();