1

I want to iterate a collection which starts at collection document ID.

Is there any method like below which let us read data start at an id or an attribute id?

 db.collection("questions").startReadingDocumentAt("02cubnmO1wqyz6yKg571 ").limit(10).get() 

or

db.collection("questions").startReadingWhereEqualTo("questionID","02cubnmO1wqyz6yKg571 ").limit(10).get()

I know I can read that DocumentSnapShot on basis of Id and can then start iterating with lastVisible item. but it cost me two read operation. Can it be done in one.

Firestore-root
    |
    --- questions (collections)
    |     |
    |     --- 02cubnmO1wqyz6yKg571 (questionId document) // myID and my questionID is same
    |            |
    |            --- questionId: "02cubnmO1wqyz6yKg571"
    |            |
    |            --- title: "Question Title"
    |            |
    |            --- date: August 27, 2018 at 6:16:58 PM UTC+3
    |            |

I already have gone through this

Explanation

I don't want to do RecyclerView pagination. I want to load next 10 question each time an Activity starts. So I call the query in the same manner but it requires me to save the DocumentSnapShot. So I decided to save the questionID and read data on basis of it...

Raj
  • 2,939
  • 2
  • 10
  • 27
Zar E Ahmer
  • 32,807
  • 18
  • 222
  • 281

2 Answers2

1

There is no:

startReadingDocumentAt("02cubnmO1wqyz6yKg571")

nor

startReadingWhereEqualTo("questionID","02cubnmO1wqyz6yKg571")

In firestore but according to your database schema, to solve this, you can use the following query:

db.collection("questions").
    whereGreaterThanOrEqualTo("questionID","02cubnmO1wqyz6yKg571")
    .limit(10)
    .get()
    addOnCompleteListener.(/* ... */)

More informations here:

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than or equal to the specified value.

Alex Mamo
  • 112,184
  • 14
  • 139
  • 167
1

You can do this using query cursors. E.g.:

db.collection("questions")
    .orderBy("questionID")
    .startAt("02cubnmO1wqyz6yKg571")
    .limit(10)
    .get()
Michael Lehenbauer
  • 16,031
  • 1
  • 55
  • 59