Home > database >  Query in MongoRepository with Limit
Query in MongoRepository with Limit

Time:10-13

@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.

CodePudding user response:

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

CodePudding user response:

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);
  • Related