« Back to full topics list
0
0

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

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

2 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
0
Private reply

With query cursors in Cloud Firestore, you can part information returned by a query into clumps as indicated by the parameters you characterize in your query.

Query cursors characterize the begin and end focuses for a query, enabling you to:

Restore a subset of the information.

Paginate query comes about.

Utilize the startAt() or startAfter() strategies to characterize the begin point for a query. The startAt() strategy incorporates the begin point, while the startAfter() technique avoids it.

You can likewise pass a documented snapshot to the cursor statement as the begin or end purpose of the query cursor. The qualities in the document snapshot fill in as the qualities in the query cursor.

Elizabeth
writer@ https://supremeessaywritingservice.com/

Marked as spam
Posted by - Replied on January 17, 2018 7:16 am