Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a number of document from an index to other index by query in Firestore

I want to make pagination with data what is stored in cloud Firestore. It looks like that:

enter image description here

Note, It is different from load more or infinite loading. Lets assume that, When user click at pager 3, I need to make a request to my Firestore to get 25 items from position 50 to 75(25 items per page).

I should do like that:

db.collection("cities")
    .orderBy("population")
      .startAfter(lastVisible)
        .limit(25);

The problem here is I do not have lastVisible. I know we can get lastVisible by getting first 50 items but it is so dumb because if I do like that, why don't I just get first 75 items and filter what we want in client.

The question is: How to get a number of items from an index to other index by query in Firestore?

Any help would be appreciated! Thanks so much!

like image 377
Duannx Avatar asked Jan 12 '18 04:01

Duannx


1 Answers

Updated answer:

You can maintain an array of size as the decided count of your pages. Value at each index would denote the startAt field value of your cities field.

Additionally you will also maintain a count of your documents. This will help you give the limit value (count / numberOfPages).

You will maintain both of these with help of cloud function triggers, as already described here.

like image 188
Abdul Wasae Avatar answered Oct 12 '22 14:10

Abdul Wasae