0

This is the Firestore collection with custom document IDs which are actually dates as string. The purpose of this kind of custom id is fetching the documents of particular date.

enter image description here

Here's the code :

FirebaseFirestore.getInstance().collection("Users")
                    .document(FirebaseAuth.getInstance().getUid())
                    .collection("Bookings By Date")
                    .get().addOnCompleteListener(new 

OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "onComplete: called");
                        Log.d(TAG, "onComplete: task result = " + task.getResult());
                        Log.d(TAG, "onComplete: task.getResult().getDocuments().size() = " + task.getResult().getDocuments().size());
                        QuerySnapshot queryDocumentSnapshots = task.getResult();

            }
        }
    });

enter image description here Here are Logcatlogs where task.getResult().getDocuments().size() is returning 0 it should return 3 as i have 3 documents in the collection.

Ssubrat Rrudra
  • 870
  • 8
  • 20

2 Answers2

3

Issue resolved. If a firestore document doesn't have any field in it then CollectionReferece#get() method skip those documents.

enter image description here

Ssubrat Rrudra
  • 870
  • 8
  • 20
3

A document can actually be empty in Firestore, meaning that it is possible to have a document without data, and your code would load that.

In this case however, in your screenshot you can see that 1-4-2019 document name is is displayed in italics, which means that this document doesn't exist. The console merely shows this name in the list, because there are subcollections under that path. And since document 1-4-2019 doesn't exist, it won't be returned when you load all documents.

In short:

  • A document can be empty, meaning that you can have a document without any data fields.
  • All documents (including empty ones) are returned when you read all documents from a collection.
  • If there are subcollections under a path for which no document exists, the Firestore console will show that non-existing document's title in italics.
  • Non-existing documents are not returned when you read all documents from a collection.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Well, I could not even set the document fields programmatically. For this particular task where I can retrieve subcollections of a particular document by just referring its custom document ID, I had to use old school Firebase. – Ssubrat Rrudra Mar 09 '19 at 19:53
  • The code in your question shows nothing about writing. I answered your original problem, explaining why you get 0 documents when you read the documents from the collection. In addition I tried to explain why your fix may work, Firestore **can** read empty documents. – Frank van Puffelen Mar 09 '19 at 20:02