Results 1 to 6 of 6

Thread: Pageable and custom queries

  1. #1

    Default Pageable and custom queries

    Hi.

    I'm using spring-data-mongodb M3. I like the idea of Pageable and this seems to work with auto-query-generation from methods that have Pageable in their signature.
    Now I have some custom query methods too where I have to use the mogo template for building complex queries. I would like to pass the Pagable object to my
    query methods too to read page, size, order etc. from it easily. But looking at the Query object there is no such method like setPageable or fromPageable or similar.
    So I have to write something like this:

    query.limit(pageable.getPageSize());
    query.skip(pageable.getOffset());
    ...

    It would be great if I simply could pass the Pageable object to the Query so constraint are read from there automatically.
    Also I didn't manage to put the sort criteria from the Pageable to the Query. Since I can't read the property from the Pageable somehow.

    Are there plans to add those features in future versions?

  2. #2

    Default

    If you are using repositiries, you can try something like this:

    @Query("{ 'state' : { '$regex' : ?0 , '$options' : 'i'} }")
    Page<Location> findByStateLike(String state, Pageable pageable);

  3. #3

    Default

    Yes, I'm aware of the possibility to use Pageable within auto-generated-query methods. But I'm missing it within REAL custom methods. Like thisIsMyCustomQueryMethod including an implementation where code is accessing MongoTemplate directly.

  4. #4
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    Have a look at the ….mongodb.repository.QueryUtils. It pretty much offers exactly what you're looking for. applyPagination(…) already transparently adds sorting information contained in the given Pageable as well.

  5. #5
    Join Date
    Dec 2008
    Location
    Aurora, CO, USA
    Posts
    24

    Default

    Quote Originally Posted by Oliver Gierke View Post
    Have a look at the ….mongodb.repository.QueryUtils. It pretty much offers exactly what you're looking for. applyPagination(…) already transparently adds sorting information contained in the given Pageable as well.
    Unfortunately, QueryUtils is package scoped to "org.springframework.data.mongodb.repository", not Public, so you can't use these methods in your custom repositories.

  6. #6

    Default

    Quote Originally Posted by johannz View Post
    Unfortunately, QueryUtils is package scoped to "org.springframework.data.mongodb.repository", not Public, so you can't use these methods in your custom repositories.
    Exactly. too bad...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •