« Back to full topics list
0
0

Hi, can you explain to me how I can paginate when querying for documents?

Marked as spam
Posted by - Asked on April 17, 2017 5:40 pm - 16 views

1 Replies

0
Private reply

Hi Stefan,

You can use the “filter” object when calling the query method of the document object. It accepts the following parameters:

– count: how many documents in total verify the query conditions
– resultsPerPage: how many documents to return on every execution of the same query conditions, defaults to 50
– pageNumber: the number of the page to return when count > resultsPerPage

For example:

var documents = require("document");
const MAX_PAGES = 5;
// ask to load all documents in my store but only return first 5 documents
queryResponse = documents.query({
		"query" : '',
		"fields" : "*",
		count : "true",
		resultsPerPage : MAX_PAGES
	});
// display results, i.e. 5 documents, in the console
console.log("PAGE 1 n" + JSON.stringify(queryResponse.result.documents));
if (queryResponse.result.count > 0 && queryResponse.result.count > MAX_PAGES) {
	// if more than 5 documents are available that satisfy our query condition, ask to load the next page (page 2)
	queryResponse = documents.query({
			"query" : '',
			"fields" : "*",
			count : "true",
			pageNumber : 2,
			resultsPerPage : MAX_PAGES
		});
	// display next 5 documents in the console
	console.log("PAGE 2 n" + JSON.stringify(queryResponse.result.documents));
}

Hope this helps.

Regards, Julien Mrad Scriptr.io

Marked as spam
Posted by - Replied on April 18, 2017 2:13 pm
« Back to full topics list