How to access a specific field from Cloud FireStor

2019-01-12 11:28发布

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.

3条回答
老娘就宠你
2楼-- · 2019-01-12 11:59

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")
            }
        }
查看更多
Evening l夕情丶
3楼-- · 2019-01-12 12:12

You can create new privates collections inside the main collection, and set different rules for access.

Check at: Firebase Firestore get private fields

Hope I helped you.

查看更多
时光不老,我们不散
4楼-- · 2019-01-12 12:22

There is no API that fetches just a single field from a document. 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.

查看更多
登录 后发表回答