1

I want to get a document by index in my Firestore database.

The only solution I've found is by finding it through the documents produced by

db.collection("Users")
  .document((Auth.auth().currentUser?.uid)!)
  .collection("Posts")
  .order(by: "updatedDate")
  .limit(to: numberOfEvents).getDocuments

Is there any other way to do this?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • You will have to define what you mean by "get a document by index". What you've shown queries documents by a defined field order, limited by a certain number of documents. – Doug Stevenson Jun 01 '19 at 03:11
  • There is no support for getting a document by its offset from the start of a result set in the iOS SDK for Firestore. Only `startAt()`/`startAfter()` are supported, which take values of the fields you order for as their arguments. The only Firestore SDKs that support index based offsets are the server-side/Admin SDK, and even there I'd recommend against using them as the implementation is likely to lead to billing surprises (as it reads all documents before the offset too). See https://stackoverflow.com/a/48887888, https://stackoverflow.com/a/50331532 – Frank van Puffelen Jun 01 '19 at 03:38
  • Technically one solution, but not recommended, is to store the documents in an array, which naturally have an index. The code included in your question doesn't have any kind of index and is ordered by updatedDate so if an updateDate date changes to a more current date, that will change the ordering, therefor changing it's 'index' in the list. Can you provide more information? – Jay Jun 01 '19 at 13:39

0 Answers0