Friday 15 January 2010

Is it possible to use SpringData MongoDB repository to execute an arbitrary query, with pagination? -


We have a usage case where a user can pass in arbitrary search criteria for a single archive, and endorse production Using Spring Data Treasures, it is very simple if we know ahead of time that they are searching by simple expansion MongoRepository, and can declare:

Pages & Lt; Thing & gt;

However, if we use either the fluent interface ourselves or build a Mongo string and wrap it in the code, then findfuendbase (type foo, type bar, Type False, pageable page)> Basic equation class, I can not find a way to get it in a repository instance. Not:

Page & lt; Thing & gt;

The functionality which is able to see me.

And I can not see that MongoTemplate functionality inquiry with page separation

I hope I do not have to do my paging role (to leave Calculate and limit parameters, which I think is not that difficult) and call directly on the template, but I think that if this be the best option.

I do not think this can be done the way I hope, but I Come up with an alternative solution. As a backdrop, we have access to data in DAO, and some representatives of the repository, put all our methods in order to do something in the template.

  • A DAO method is written which is our arbitrary filter string (which I have a utility that converts it to the standard standards in the Jasonan query syntax.
  • A "content" Wrap the basic information to get.
  • Using the calculation, the total number of records using MongoTemplate # count (query, class)
  • My paging honorary to create a "pagecauli" using with query # (pageable)
  • Turn on the page content with MongoTemplate # find (query, page)
  • Get the list The result from that, was used for that query and returned count by counter-call run, and on the caller To return, create a new PageImp .

    Actually, this ( DocDbDomain document is a test domain for testing DB stuff Class):

      query countQuery = new default (toMongoQueryString (filterString)); Query page page = queryQuery.with (pageRequest); Long Total = Template Coat (Calculation, DocDbDomain.class); & Lt; DocDbDomain & gt; Content = template.find (PageLine, DocDbDomain.class); New Pageimple return & lt; DocDbDomain & gt; (Content, page confirmation, total);    

No comments:

Post a Comment