问题
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