I'm trying to read a specific field from all documents in a collection without listing all fields. For example, I want to only read and print Brand from all documents in Donut collection. But right now my python script is printing all fields in all documents (code obtained from: https://firebase.google.com/docs/firestore/quickstart). I couldn't find the answer in it. can someone help?
Asked
Active
Viewed 1,579 times
3

Shanely
- 103
- 12
-
I don't think this is possible through the Admin SDKs (I'm sure it is not possible through the Node.js Admin SDK). However it is possible with the Firestore REST API with a [DocumentMask](https://cloud.google.com/firestore/docs/reference/rest/v1/DocumentMask) – Renaud Tarnec Mar 01 '21 at 08:25
2 Answers
3
Try the codes below, it will print all Brand from all documents in Donut collection:
users_ref = db.collection('Donut')
docs = users_ref.stream()
for doc in docs:
brand = u'{}'.format(doc.to_dict()['Brand'])
print(brand)

JM Gelilio
- 3,482
- 1
- 11
- 23
1
If you just want one field you can use the get() method of the DocumentSnapshot. The code below should work for what you want:
users_ref = db.collection('Donut')
docs = users_ref.stream()
for doc in docs:
brand = doc.get('Brand')
print(brand)

M Saavedra
- 11
- 1