0

We are developing a mobile application where are using firebase as backend. We are using cloud firestore as our database. While querying data from Android it return blank JSON. Here is how our database looks

enter image description here

Here is our android code

        db.collection("meta_data")
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        Log.d(TAG, document.getId() + " => " + document.getData());
                    }
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });

It prints

college_metadata => {}

1 Answer 1

2

Your "college_metadata" document doesn't actually contain any fields. It's what I would call an "empty document". If you print the data from an empty document, you will get an empty map, which is what you're seeing in your output: {}.

Queries in Firestore are "shallow", which means they don't fetch nested subcollections of matched documents. If you want the data in a nested subcollection, you will have to query for it by name:

db
    .collection("meta_data")
    .document("college_metadata")
    .collection("course")
    .get()

This will give you all the documents in the "course" subcollection of the empty document named "college_metadata".

There is no way to make a Firestore query get all nested documents in all nested collections. You need to write that code, if that's what you want.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, What if I would like to fetch all collection from college_metadata
It's not possible with one query. You have to query each nested subcollection individually.
Is there any tutorial or references for the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.