18

Here is my data structure:

data structure

I have an ios app that is attempting to access data from Cloud Firestore. I have been successful in retrieving full documents and querying for documents. However I need to access specific fields from specific documents. How would I make a call that retrieves me just the value of one field from Firestore in swift? Any Help would be appreciated.

toddg
  • 2,863
  • 2
  • 18
  • 33
Kunwar Sahni
  • 194
  • 1
  • 1
  • 8

5 Answers5

21

There is no API that fetches just a single field from a document with any of the web or mobile client SDKs. Entire documents are always fetched when you use getDocument(). This implies that there is also no way to use security rules to protect a single field in a document differently than the others.

If you are trying to minimize the amount of data that comes across the wire, you can put that lone field in its own document in a subcollection of the main doc, and you can request that one document individually.

See also this thread of discussion.

It is possible with server SDKs using methods like select(), but you would obviously need to be writing code on a backend and calling that from your client app.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 3
    This feature should be added in the Mobile SDK. – wonsuc Mar 20 '19 at 13:39
  • 2
    @wonsuc Feel free to file a feature request. http://firebase.google.com/support/contact/bugs-features – Doug Stevenson Mar 20 '19 at 16:01
  • There is indeed a `_projection` property on firestore.Query. I tried to assign a fields list to it, but it rise the following exception: `VIRTUALENV/env/lib/python3.7/site-packages/google/cloud/firestore_v1/base_query.py in _normalize_projection(projection) ... 684 if projection is not None: ... --> 686 fields = list(projection.fields) ... AttributeError: 'list' object has no attribute 'fields'` – user2154587 Aug 11 '21 at 12:42
11

This is actually quite simple and very much achievable using the built in firebase api.

        let docRef = db.collection("users").document(name)

        docRef.getDocument(source: .cache) { (document, error) in
            if let document = document {
                let property = document.get(field)
            } else {
                print("Document does not exist in cache")
            }
        }
Anthony Sobo
  • 377
  • 3
  • 4
5

There is actually a way, use this sample code provided by Firebase itself

let docRef = db.collection("cities").document("SF")

docRef.getDocument { (document, error) in
    if let document = document, document.exists {
        let property = document.get('fieldname')
        print("Document data: \(dataDescription)")
    } else {
        print("Document does not exist")
    }
}
Yash Kothari
  • 233
  • 3
  • 6
0

I guess I'm late but after some extensive research, there is a way in which we can fetch specific fields from firestore. We can use the select keyword, your query would be somthing like (I'm using a collection for a generalized approach):

const result = await firebase.database().collection('Users').select('name').get();

Where we can access the result.docs to further retrieved the returned filtered result. Thanks!

Thomas David Kehoe
  • 10,040
  • 14
  • 61
  • 100
-3
//this is code for javascript
var docRef = db.collection("users").doc("ID");

docRef.get().then(function(doc) {
if (doc.exists) {
//gives full object of user
console.log("Document data:", doc.data());
//gives specific field 
var name=doc.get('name');
console.log(name);

} else {
    // doc.data() will be undefined in this case
    console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
  • 6
    Didn't the question specifically ask for a Firebase/Swift solution? –  Mar 05 '19 at 18:02