Alex
Alex

Reputation: 1486

Firestore query returns empty JSON object

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 => {}

Upvotes: 0

Views: 1034

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317352

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.

Upvotes: 2

Related Questions