convert List<User> users to Page<User> in spring data mongo Pagination

See original GitHub issue

I am really struggling to convert List<User> users to Page<User> in spring data mongo? Note Page is an API from org.springframework.data.domain.Page;

I am using Pagination of Spring Data Mongo, so I need to sent Page<User> and not the List<User>. Please help me.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

30reactions
eugenpcommented, Sep 15, 2015

The easiest way to do that is to simply create a page out of the existing list: new PageImpl<Foo>(listOfFoos, new PageRequest(page, size, sort), listOfFoos.size()) Hope it helps. Cheers, Eugen.

9reactions
gssitraineecommented, May 30, 2018

I tried the suggestion above but it seems that the totalElements, totalPages values are wrong. It displays all the results… numberOfElements value is not changing… while totalElements is varying.

lets say the number of results is 7.

current page is 1 and size is 5… “first”: true, “last”: false, “number”: 0, “numberOfElements”: 7, “size”: 5, “totalElements”: 7, “totalPages”: 2

current page is 2 and size is 5… “first”: false, “last”: false, “number”: 1, “numberOfElements”: 7, “size”: 5, “totalElements”: 12, “totalPages”: 3

current page is 3 and size is 5… “first”: false, “last”: false, “number”: 2, “numberOfElements”: 7, “size”: 5, “totalElements”: 17, “totalPages”: 4

current page is 4 and size is 5… “first”: false, “last”: false, “number”: 3, “numberOfElements”: 7, “size”: 5, “totalElements”: 22, “totalPages”: 5

It is really acting weird. below is the code used

    public Page<Ticket> getOwnedTicketList(Pageable pageable) throws IOException {
        User user = getCurrentUser();
        List<Ticket> tickets = m_ticketRepository.getTicketsOwned(user);
        Page<Ticket> page = new PageImpl<>(tickets,
                new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort()),
                tickets.size());
        return page;
    }

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert List to Page in spring data mongo? - Stack Overflow
It very simple implementation and works fine for me. Page<User> usersPage = new PageImpl<User>(users,pageable, users.size());.
Read more >
Mongodb – Convert List to Page in spring data mongo
I am really struggling to convert List<User> users to Page<User> in spring data mongo? Note Page is an API from org.springframework.data.domain.Page ;.
Read more >
[Solved]-Convert List to Page in spring data mongo?-mongodb
You can do it in following way. It very simple implementation and works fine for me. Page<User> usersPage = new PageImpl<User>(users,pageable, users.size()) ...
Read more >
Paging with Spring Boot - Reflectoring
An in-depth look at the paging support provided by Spring Data for querying Spring Web MVC controllers and Spring Data repositories.
Read more >
Spring Boot MongoDB Pagination example with Spring Data
findAll(Pageable pageable) : returns a Page of entities meeting the paging condition provided by Pageable object. Spring Data also supports many ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found