Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring java MongoDB @Query annotation for order top or first records

I use @Query annotation and crudRepository to fetch data from MongoDB. Example:

@Query("{$query: id: ?0}, $orderby: {dateTime: -1}}")
public List<A> findId(
    Integer id, Pageable pageable
);

What do I need to add to my @Query notation if i want to fetch only first N records of collection, or only top N records of collection? I would like to use dynamic query and dynamic set limit and order (first or top records).

like image 650
Dmitry Igumnov Avatar asked Oct 20 '25 09:10

Dmitry Igumnov


1 Answers

you can append your @Query with $limit : 10, or rename your method like : find First10ByDateDesc()

like image 185
Zava Avatar answered Oct 21 '25 23:10

Zava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!