How to make a custom sorting query in spring boot for a mongo db repository?

蹲街弑〆低调 提交于 2019-12-07 07:58:40

问题


I want to put this query with @Query annotation in my repository.

This is the query:

`db.report.find({'company' : 'Random'}).sort( { 'reportDate' : -1} ).limit(1)`

Which is the best way to implement custom queries with @Query annotations or to use MongoTemplate ?


回答1:


Using Mongo Template.

Criteria find = Criteria.where("company").is("Random");
Query query = new Query().addCriteria(find).with(new Sort(Sort.Direction.DESC, "reportDate"));
BasicDBObject result = mongoOperations.findOne(query, BasicDBObject.class, "collection_name");

Using Mongo Repository

Report findTopByCompanyOrderByReportDateDesc(String company)


来源:https://stackoverflow.com/questions/41806584/how-to-make-a-custom-sorting-query-in-spring-boot-for-a-mongo-db-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!