skip to Main Content

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())

log response

the database

values from DatabaseConstants class

2

Answers


  1. You are getting zero because there are no documents inside the All_Games collection. Why do I say that? It’s because the ac53...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 following CollectionReference object:

    val allGamesDocumentSnapshot = firestore
                 .collection(DatabaseConstants.ALL_GAMES)
                 .document("ac53...c880")
                 .collection(DatabaseConstants.Game_Data)
                 .get()
                 .await()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search