I’m trying to fetch multiple document IDs from my collection so I can further access the collection within those documents, but when I try to check the documents.size it says that the size is 0 even though I have data in my Firebase Collection.
val allGamesDocumentSnapshot = firestore.collection(DatabaseConstants.ALL_GAMES)
.get()
.await()
Log.v(TAG, allGamesDocumentSnapshot.documents.size.toString())
2
Answers
You are getting zero because there are no documents inside the
All_Games
collection. Why do I say that? It’s because theac53...c880
document doesn’t exist. That’s the reason why it is displayed in italics.If you want to calculate the number of documents inside the collection, you have to add at least a field inside the document. I see that inside the document there is a sub-collection. If want to the documents that exist inside the
Game_Data
sub-collection, then you have to create a reference that points to that sub-collection. This is because the queries in Firestore are shallow, they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and sub-collections in one go.Edit:
To create a reference that points to
Game_Data
sub-collection, please use the followingCollectionReference
object:Refer to the below documents to get an idea
https://firebase.google.com/docs/firestore/query-data/get-data#initialize