0
@Query(value = "{$and:[{'contentRoot.basicData.code':{$ne:null}}" +
        ",{'contentRoot.basicData.tourOperatorCode':{$ne:null}}]}"
        ,fields = "{$and: [{productCode: 1, tourOperatorCode: 1}]}")

I try to get 3 Documents from Mongodb. I don´t know where can i use {$limit: 3}, or alternative command.

2 Answers2

0

You can use it at the end of the query.

@Query("{'source':?0,'target':?1}", sort = "{'date': -1}", limit = 1)

Or you can use the @Aggregation annotation

read this for more details. https://stackoverflow.com/a/71292598/8470055

  • It doesn´t work with @Query(value=....) – Artem Brazhnyk Oct 12 '22 at 13:02
  • The code snippet here seems to be from a 10 year old github issue [here](https://github.com/spring-projects/spring-data-mongodb/issues/1492#issuecomment-752649068). Was that feature implemented with that syntax, does it still work that way, and how can the OP use that for their particular question? – user20042973 Oct 17 '22 at 22:46
0

You can provide Pageable as query method parameter

@Query(value = "{$and:[{'contentRoot.basicData.code':{$ne:null}}" +
        ",{'contentRoot.basicData.tourOperatorCode':{$ne:null}}]}"
        ,fields = "{$and: [{productCode: 1, tourOperatorCode: 1}]}")
List<Object> findTop3Objects(Pageable pageable);

And init Pageable this way:

Pageable pageable = PageRequest.ofSize(3);
artiomi
  • 383
  • 2
  • 7