Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore get DocumentSnapshot's field's value

If I have a Firebase Firestore database which I have retrieved a DocumentSnapshot for the document corresponding to the collection on the right and stored in a document variable, then how could I retrieve the value in that DocumentSnapshot at the field "username"? The field has a string value.

enter image description here

like image 519
Paradox Avatar asked Jan 29 '18 00:01

Paradox


People also ask

How do I get a specific value from firestore?

To order the documents by a specific value, use the orderBy method: firestore() . collection('Users') // Order results . orderBy('age', 'desc') .

How do I get data from QueryDocumentSnapshot?

A QueryDocumentSnapshot contains data read from a document in your Cloud Firestore database as part of a query. The document is guaranteed to exist and its data can be extracted using the getData() or the various get() methods in DocumentSnapshot (such as get(String) ).

How do I get firestore react document ID?

To query Firestore database for document ID with JavaScript, we call the doc method. Then we call doc to get the entry from the books collection with ID 'fK3ddutEpD2qQqRMXNW5' . Finally, we call get to return the object with the given ID.


3 Answers

DocumentSnapshot has a method getString() which takes the name of a field and returns its value as a String.

String value = document.getString("username"); 
like image 169
Doug Stevenson Avatar answered Sep 25 '22 07:09

Doug Stevenson


you can use get method to get value of a field

String username = (String) document.get("username");  //if the field is String
Boolean b = (Boolean) document.get("isPublic");       //if the field is Boolean
Integer i = (Integer) document.get("age")             //if the field is Integer

checkout the doc for DocumentSnapshot

like image 42
Ali Faris Avatar answered Sep 22 '22 07:09

Ali Faris


You need to do a DocumentReference to get the content in your document.

A simple one will be like this.

DocumentReference docRef = myDB.collection("users").document("username");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
     public void onComplete(@NonNull Task<DocumentSnapshot> task) {
          if (task.isSuccessful()) {
               DocumentSnapshot document = task.getResult();
                    if (document != null) {
                         Log.i("LOGGER","First "+document.getString("first"));
                         Log.i("LOGGER","Last "+document.getString("last"));
                         Log.i("LOGGER","Born "+document.getString("born"));
                    } else {
                         Log.d("LOGGER", "No such document");
                    }
               } else {
                    Log.d("LOGGER", "get failed with ", task.getException());
                }
          }
     });

The downside is that you need to know your document ID to get the field values.

like image 24
Steffo Dimfelt Avatar answered Sep 23 '22 07:09

Steffo Dimfelt