2

Is there the ability with Firebase Firestore to only get one field from a document?

public String getUserName(){
    DocumentReference docRef = db.collection("cities").document(mAuth.getUid());
    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.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
                } else {
                    Log.d(TAG, "No such document");
                }
            } else {
                Log.d(TAG, "get failed with ", task.getException());
            }
        }
    });

    return name;
}

I use the above code to retrieve a Map with all data in it but would like to know if there is functionality to just call a field name and retrieve that fields data?

My Firestore database is below. I would like to be able to retrieve the Name field only.

enter image description here

Matt List
  • 1,825
  • 1
  • 21
  • 25
  • The marked duplicate is for Swift, but the same applies for all client platforms. – Doug Stevenson Mar 01 '18 at 21:49
  • Thanks for that. Just to confirm I understand what the switft post states. I should actually be creating all my users as collections and having documents as the fields? Example Collection: User 1. Document: Name. Field: Matt Collection: User 1. Document: Email. Field: test@test.com – Matt List Mar 01 '18 at 22:00
  • Not sure what you're asking here. It doesn't seem to be related to your original question. If you have a question about data modeling, that would be a different question. – Doug Stevenson Mar 01 '18 at 22:07
  • What i am asking is if i should be storing my fields shown in the above screen shot as documents? – Matt List Mar 01 '18 at 22:08
  • Sure, if it works for your queries. Data modeling for nosql databases is entirely dependent on how you intend to query for that data. It's up to you. – Doug Stevenson Mar 01 '18 at 22:11

0 Answers0