0

I've got a collection named contactData, inside this collection I have 100 documents.

Inside my flutter application I'm generating 4 random numbers from 0 to 100. Afterward I'm trying to read from my collection documents at these indexes. But it seems that I can refer only to the document name.

Is there a way to grab the documents by it's index in collection without providing index inside each document?

If this question doesn't align with the terms of use of this website, please, delete it.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mr.BitCoin
  • 23
  • 4
  • in my opinion it is IMPOSSIBLE, simply you cannot query data by index without having them on firebase – Bawantha Jul 14 '20 at 13:33

1 Answers1

0

Firestore queries are not index based, as that just doesn't match with the realtime nature of the database. To read document N+1, you'll need to also read all N documents before it.

The server-side SDKs for Firestore do have the necessary offset API, but that internally does precisely what I described already: it reads all N+1 documents, and returns only the last one of that sequence.


If you need a ranking system, you'll indeed want to store the ranking inside each document. Have a look at this explanation from Dan on how to build such a system on Firestore: Leaderboard ranking with Firebase


If you want to return a random document and use your random numbers as the basis for that, that is possible. Take a look at (again) Dan's explanation about how to do that here: Firestore: How to get random documents in a collection

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807