0

I'm trying to get every field in one document in firestore. Field's key are random numbers and values are same with keys.How can i do that with document?

DocumentReference docRef = db.collection("cities").document("SF");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
    if (task.isSuccessful()) {
        DocumentSnapshot document = task.getResult();
        if (document.exists()) {
            Log.d(TAG, "DocumentSnapshot data: " + document.getData());
            // get every field in document.getData();
        }
    } 
}
});
Kai Tera
  • 59
  • 2
  • 9

1 Answers1

2

document.getData() returns a Map<String, Object> with all the fields from the document, as you can see from the API documentation. You iterate all the fields in the same way that you iterate all the entries in a Map.

for (Map.Entry<String, Object> entry : map.entrySet()) {
    // entry.getKey() will contain the name of the field with each iteration.
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441