find the last_id in the current page - mongodb

Can somebody help me out how to find the last_id in the current page? I am displaying 50 records inside a table using the query db.users.find().limit(50)
I want the id of the 50th record of the page and want to use it in the bellow query:
users = db.users.find({'_id'> last_id}). limit(50)
Thanks!

I guess your intent is to do pagination with MongoDB. We have better ways to do that. For instance:
//Page 1
db.users.find().skip(0).limit (10)
//Page 2
db.users.find().skip(10).limit(10)
//Page 3
db.users.find().skip(20).limit(10)
Change the skip value based on the page you are showing (i.e. (N-1) * 10).
Learn more.

Related

Is there a way to see if a limit offset query has reached the end with pgpromse?

I have a table of posts. I would like to query these posts as pages. Because I would like to keep my endpoints stateless I would like to do this with offset and limit like this:
SELECT * FROM post LIMIT 50 OFFSET $1 ORDER BY id
Where $1 one would be the page number times the page size (50). The easy way to check if we have reached the end would be to see if we got 50 pages back. The problem of course is if the number of pages is divisible by 50, we can't be sure.
The way I have solved this until now is by simply fetching 51 posts per query with the page size still being 50. That way if the return query is less than 51, we have reached the end.
Unfortunately, this seems a very hacky way to do this. So I was wondering, is there some feature within pg-promise or postgresql that would indicate that I have reached the end of a table without resorting to tricks like this?
The simplest method with the lowest overhead I found:
You can request pageLimit+1 rows on every page request. In your controller you will check if rowsCount > pageLimit and will know that there is more data available. Of course, before returning the rows, you would need to remove the last element and send along the rows something like a hasNext boolean.
It is usually way cheaper for the DB to retrieve an extra row of data than count all rows or make an extra request for page+1 to check if it returns any rows.
Well there is no built in process for this directly. But you can count the rows and add that to the results. You could then even give the user the number of items or number of pages:
-- Item count
with pc(cnt) as (select count(*) from post)
select p.*, cnt
from post p
cross join pc
limit 50 offset $1;
-- page count
with pc(cnt) as (select count(*)/50 + ((count(*)%50)>0)::int from post)
select p.*, cnt
from post p
cross join pc
limit 50 offset $1;
Caution: The count function can be slow, and even when not it does add to response time. Is it worth the additional overhead? Only you and the user can answer that.
This method works well only in specific settings (SPA with caching of network requests and desire to make pagination feel faster with pre-fetching):
One every page, you make two requests: one for the current page data and one for the next page's data.
It works if you for example use a React Single-Page Application with react-query where the nextPage will not be refetched but reused when user opens it.
Otherwise, if the nextPage is not reused, it's worse than checking for a total number of rows to determine whether there are any rows left as you will make 2 requests for every page.
It will even make the user interface snappier as the transition to the next page will always be instant.
This method will work well if you have a lot of page transitions as the total number of calls equals numberOfPages+1, so if on average users go to 10 pages, numberOfPages+1=10+1 or just 10% overhead. But if your users usually do not go beyond the first page, it makes little sense as in this case numberOfPages+1=2 calls for a single page.

Mongo DB skip and limit

I want 10,000 rows in a collection. I want to display 10 records first and when user clicks next, next set of 10 records and when user clicks next, next set of 10 records and goes on...
Example or Expected Result
// Page 1
db.students.find().limit(5)
// Page 2
db.students.find().skip(5).limit(5)
// Page 3
????
The page 3 specified in the web-site doesn't show a different query instead the same skip and limit is performed.
https://www.codementor.io/arpitbhayani/fast-and-efficient-pagination-in-mongodb-9095flbqr
My question is do we need to execute the following query for page 3 ???
// Page 3
db.students.find().skip(10).limit(5) (OR)
db.students.find().skip(5).limit(5) as specified in the link
Thanks.
const limit = 10 // Numbers of items you want to display per page
const page = 1 // This value should change dynamically when user select other page
const skip = (page - 1) * limit // for skipping the items
This Example will show exactly 10 elements (if enough items are available in your database) of 1st page without skipping any elements (skip = 0).

Spring JPA : how can i get the last Page of an object when I do not know the total number of page

I am trying to get the total number of pages for my Object, so that i would be able to call the last page. This was the code i used
Page<Message> firstPage = defaultMessageService.getMessage(user, user2, new PageRequest(0, 6));
paginationModel.setMessagesPage(defaultMessageService.getMessage(user, user2, new PageRequest(firstPage.getTotalPages() - 1, 6)))
.setResponseMessage("Successful")
.setSuccessful(true);
in my code i called the first page, just to get the total number of pages and subtracted the the total pages by one, to get the last page. This is resource taking as i get the list of content with the page, contents i dont intend to use. Is there a better way ? i just want the total page, size, current page
if you want only last page then sort your data in descending order and then get the first page. is it possible to sort your data then your problem will be solved.

How to get current page number in sap ui5 Table Pagination?

Can anyone help me to get current page number in sap ui5 Table Pagination .
For example if am in second page in pagination I have to get count of 2 if it is third page i have to get count of 3.
Hi krishnakumar sasthaiyan,
First get the ID of the table, and below line of code should work.
sap.ui.getCore().byId("TABLE_ID")._oPaginator.getCurrentPage();

Range query with no dups

I have a collection that I would like to serve out as 'pages'. The collection could get quite large, I have read skip is not optimal in that case. I think range queries will work just fine in my case so I am going to try that route.
My collection will be sorted and paged on a timestamp field. I have implemented the API such that a user passes in a startDate and I will return a certain number ('limit', max of 1000) of items. However I am struggling with how to not get duplicates on each page if documents have the same time.
As an example (small page size to make it easy).
I have 6 documents let's docs 3 and 4 have the same time. If I ask for page one I will get the first three. However when I ask for page 2 with a startDate that it 'gte' the last doc on page one I will get a dup on page 2 as the last doc from page one will be that same as the first doc on page 2.
I cannot find a range query example anywhere that deals with dates, while not returning dups.