skip to Main Content
@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

Answers


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

    Login or Signup to reply.
  2. 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search