1
votes

I'm trying to add a nice feature to my very first and elementary app that connects to Firestore database, gets all the names of the documents in the collection there and stores them in an ArrayList of strings. As shown below, the database is quite simple and contains one collection named pedestals which includes only 3 documents. Each document has 4 fields (jack, jack_laser, name and sum): Firestore snapshot of my database

By using pedestals.whereEqualTo("name", true).get() I guess a query for all the names is initialized but I'm not sure how to extract the names out of it. I watched many youtube videos and tried the google code-lab section for firestore, but each of them deals with querying for documents and storing their data - While here I don't want to get all the documents, only the values of their name fields.

Here is the relevant method code:

 private void myFirstQuery2() {
    // Get a reference to the pedestal collection
    final CollectionReference pedestals = mFirestore.collection("pedestals");

    // get all the names of the pedestals in firestore
    pedestals.whereEqualTo("name", true).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            for (QueryDocumentSnapshot document : task.getResult()) {
              //  names.add(document.getString("name"));
                  names.add(document.getData().toString());
            }
            String str = Arrays.toString(names.toArray());
            Log.e(TAG, "god job: " +str);
            Toast.makeText(FileLoading.this, "success!", Toast.LENGTH_LONG).show();
        }//end of onSuccess
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(FileLoading.this, "error!", Toast.LENGTH_LONG).show();
            Log.e(TAG, "could not get the query");
        }
    });
};

As you may see, I tried to implement a for loop that stores each documentsnapshot (even though I'm looking for fields and not documentsm. Is it the problem?) in an array.

In the logcat terminal I get:

08-24 17:41:45.786 10738-10738/benda.leveling_v2 E/MyActivity: god job: []

which makes me understand that the arraylist is empty.

What am I doing wrong here?

1
In firestore you always get the whole document. - Constantin Beer
@ConstantinBeer Not really. Queries are shallow, meaning you only get the information of the document on that level. You don't get its subcollections. - Gaurav Mall
@GauravMall I’m talking about documents. Subcollections are collections. Didn’t say that you get the subcollections. Firestore don’t cascade. - Constantin Beer
We both said the same thing. I just misunderstood you :) Thought that by whole documents you meant the whole "staircase" of info. - Gaurav Mall
Yeah your right. It could get misunderstood like I wrote it :) my fault. - Constantin Beer

1 Answers

0
votes

While here I don't want to get all the documents, only the values of their name fields.

There is no way you can get only the value of the name property out of a document. In Coud Firestore, there are no field-level permissions or access to a document. It's the entire document, or nothing. Firestore client-side SDKs always returns complete documents. Unfortunately, there is no way to request only a part of the document with the client-side SDK, although this option does exist in the server-side SDK's select() method.

Furthermore, when using the following line of code:

pedestals.whereEqualTo("name", true)

It means that you are searching for every document in your pedestals collection where the property name holds the value of true. Since that property holds a String and not a boolean, there are no results that can be returned. A possible query would have been:

pedestals.whereEqualTo("name", "site3")

Which it will returned the third document in your screenshot. If you want to get the name property or the id of documents, please use the following lines of code:

pedestals.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.getString("name"));
                Map<String, Object> map = document.getData();
            }
        }
    }
});

If you want to get those arrays, please note that even if in the database those properties are of type array, when you get them, the objects are of type List. Another solution might be to iterate through the map and get the corresponding values.