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).
Related
I want to get all the records on a sheet by page with such query https://api.smartsheet.com/2.0/sheets/{sheetId}?rowsModifiedSince={date}&pageSize=20&page=10
But response hasn't totalPages or similar parameter. How to find a total number of pages in a sheet?
Of course, I can check the number of rows on the current page. If it is equal to the 'pageSize' parameter then the next sheet exists. This does not seem to be correct as on the sheet can be a multiple of the number of rows.
The Get Sheet response contains a totalRowCount parameter that specifies the total number of rows in the sheet. You can calculate the total number of pages (i.e., the total number of Get Sheet requests that you'll need to submit to retrieve all rows in the sheet) by dividing the totalRowCount value by the pageSize value you're specifying in the request query string, and rounding the result up to the next whole number.
For example, if:
Your request specifies pageSize=20 (e.g., https://api.smartsheet.com/2.0/sheets/{sheetId}?pageSize=20&page=1).
And the value of the totalRowCount parameter in the Get Sheet response is 105.
This means you'll need to submit 6 Get Sheet requests (each one specifying pageSize=20) to have retrieved all rows in the sheet (because totalRowCount/pageSize = 105/20 = 5.25, rounded up to the next whole number is 6). Each of the first 5 responses (pages) would contain 20 rows, and the 6th response would contain the remaining 5 rows.
** UPDATE #1 **
If you're specifying a query string parameter (e.g., rowsModifiedSince) then the strategy above won't work -- because the value of totalRowCount always indicates the total number of rows in the sheet, which is not necessarily equal to the number of rows that meet the criteria you've specified via the query string parameter(s). In that case, you can just continue submitting Get Sheet requests, incrementing the value of the page parameter each time, until you receive a response where the rows property is an empty array (as shown in the response example below) -- this indicates that the previous page you requested was the final page of results.
{
"id": 3932034054809476,
...
"rows": []
}
** UPDATE #2 **
Unfortunately I'm seeing inconsistent behavior from Smartsheet with the approach described above.
If the last page with results is a full page of results, then the response to the request for the next page of results will return an empty rows[] array. e.g., if 4 rows meet the criteria I specify, and I specify pageSize=2 in each request -- then the first two pages will each contain 2 rows and the third page (i.e., request issued with pageSize=2&page=3 in the query string) will return an empty rows[] array.
However, if the last page with results is not a full page of results, then the response to the request for the next page of results returns the last page of results again. e.g., if 3 rows meet the criteria I specify, and I specify pageSize=2 in each request -- then the first page contains 2 rows, the second page contains 1 row, and the third page (i.e., request issued with pageSize=2&page=3 in the query string) will contain the same rows that were in the previous/last page of results -- i.e., the same 1 row that was returned for page=2.
Unfortunately, this means you may have to resort to doing something like keeping track of the row id values that you receive in each response (and if you get a response that contains a row id you've received previously, this would indicate that the page you requested previously was actually the final page of results).
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.
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.
I have a report that is only one record, however, the user specifies how many times it needs printed, let's say 10. Each time it prints it prints 4 of the same report. Like this, Page 1 x 4, Page 2 x 4, Page 3 x 4, etc. With 10 copes their will be 40 pages altogether.
Update:
I was able to get all the pages I needed based on the values the user inputs. So if there are 10 pallets I have 40 pages of labels, which is correct. Now the control number part that needs to be displayed. It is kind of like a page number but every 4 pages the number needs to increment by 1. I assume I can use WhilePrinterRecords and some how increment, but I am still new to Crystal and unfamiliar with it.
Example:
Page 1: Control number 1
Page 2: Control number 1
Page 3: Control number 1
Page 4: Control number 1
Page 5: Control number 2
Page 6: Control number 2
Page 7: Control number 2
Page 8: Control number 2
This would continue until there is no more pages to print.
if you want 4 copies for each label, what you could do is create 4 sections on the report with the same info. Make sure that you assign the label size to each page(e.g. 4x6). e.g. if you have that info on your header, create PHa, PHb, PHc and PHd, same for details or PF.
don't know if this could work for you, but at least is a recommendation. You can create suppressed sections with the same info(e.g. 10 sections) and create a parameter that control that suppress condition defaulted in 4. If the user what to print 5, it will enable section 5th, same if want to print 3 it will suppress the 4th.
Upadte: how to get the increment:
place a formula on your page footer like below one. It will return value when gets an integer and suppress it when is equal to 0(right click on it, number tab, customize, suppress if 0).
if pagenumber = Ceiling(pageNumber,4) then numbervar page := page +1
then place another formula on your PF as well like below one and suppress it when pagenumber = Ceiling(pageNumber,4) so it won't show when the other has value and put one overlapping the other to get the value in the same place.
if {1st formula} <> 0 then numbervar page1 := {1st formula};
page1 + 1
2nd Update:
Because is a label and your details are acting as a new page do this:
create a new detail section, so you will have details a and b and place your formulas on details b
I have used additional tables for such tricks; these tables must be linked to report to multiply main query results. Some care has to be taken to not allow multiple users printouts to mix.
Example, assuming your report query has some unique id to link and your user/session has also some unique id:
you create table cr_special(reportid int, userid int, ctlnumber int)
you design report, linked to this table by reportid and filtered by userid; changing ctlnumber field is taken from your special table
before printing, you delete all records with reportid/userid combo and insert new ones
you print report, giving userid as parameter
Say you want to print 10 reports with increasing numbers - then you insert 10 records into your special table with ctlnumber increasing from 1 to 10.
Variation of this solution is to use some other non-related table, like our all-dates-containing dates table :)
Both of these solutions are ugly, but they usually work.
Another approach is to use some stored procedure to return these sequential numbers as records and link your report to such procedure; Crystal Reports and stored procedures do not behave always well however.
I need to place in the header of a crystal report, the number 0 or 1.
1 if the page is the last page of the current record, else 0.
i.e I have multiple records that each have a varying number of detail lines. Each record starts on a new page with a repeating header. If the detail lines span more than one page, I need to write a 1 on the last page of the record header and a 0 on every other page header.
I've tried using the following in a formula field to detect the next record's primary key
iif((Next({primary_key}) = {primary_key}), '0', '1')
but I think this will always return 1 because it's checking the next record and not the next page's record.
Any help would be appreciated.