I was looking to see how I could introduce a sort into a Query annotation in a repository method that I have. I already saw this code in Google and here, but I could not make it works
@Query("find({state:'ACTIVE'}).sort({created:-1}).limit(1)")
Job findOneActiveOldest();
@Query("{ state:'ACTIVE', $orderby: {created:-1}, $limit:1 }")
Job findOneActiveOldest();
I know that with pagination I can make it, but in some cases I don't need paginate, so I was wondering how to make it with Query annotation.
Any suggestion please?
Just use sort parameter of @Query
annotation. 1 = ASC, -1 = DESC
@Query(
value = ...,
sort = "{'details.requestTime': -1}"
)
I don't think it is possible to do it with @Query annotation. If you dont need to paginate you can just make your repository method use Sort parameter:
@Query("{ state:'ACTIVE' }")
Job findOneActive(Sort sort);
and use it:
yourRepository.findOneActive(new Sort(Sort.Direction.DESC, "created"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With