Search code examples
python-3.xfirebasegoogle-cloud-firestore

Python + Firestore : query specific fields from all documents in a collection


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? Database Structure


Solution

  • 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)