0

I'm making some APIs that among many things are able to retrieve the respective document via a value. My data structure is as follows

Schools (collection) -> Lombardy (doc) -> Milan (collection) -> Milan (doc) - School Test1 (collection) -> 0 (doc) -> document

In the school document there is the following information:

address: School address 1
INFORMATICS {
    4A INF: "GA24HJ"
}

My goal is to retrieve the name of the school (School Test1) and the address using the code GA24HJ. My code:

firestore = firestore.client()

collections = firestore.collections()
        for doc in collections.stream():
            print(f'{doc.id} => {doc.to_dict()}')

Result:

Lombardy => {}

I want to clarify that I have not found anything useful online and that I am using Python and the admin sdk.

Matteo
  • 118
  • 2
  • 12

1 Answers1

0

According to the document structure you shared, there is no "School name". Assuming this is an error and you actually have something like this,

{
    "name": "School Test1"
    "address": "School address 1"
    "INFORMATICS": {
        "4A INF": "GA24HJ"
    }
}

you can use dot notation to query by nested fields inside map fields:

docs = firestore
.collection('Schools')
.where('INFORMATICS.`4A INF`', '==', 'GA24HJ')
.stream()

for doc in docs:
    my_school = doc.to_dict()
    print(my_school['name'])

Note that the use of CollectionRef stream() is prefered to get()

Javier Roger
  • 279
  • 1
  • 8