Home > Back-end >  How to get the name of the collection in the document
How to get the name of the collection in the document

Time:02-16

After various researches and tests I decided to write here. I am building API in Python and I am using Firebase-Admin SDK. On Firestore I have a database with many collections and subcollections and through this code I can take the documents present in the final collection. My problem / doubt is how to get the collection names. Specifically, I want to get all the names of the collections present in a document. My code:

collections = firestore.collection('schools').document(regione).collection(provincia).document(comune).collections()
for collection in collections:
    for doc in collection.stream():
        print(f'{doc.id} => {doc.to_dict()}')
       

Result: 0 => {' INFORMATICA': {'1C ART': 'test'}} school_info => {'school_name': 'Test school 1', 'indirizzo': 'Address 1'}

0 => {' INFORMATICA': {'3C ART': 'ggf8'}}
1 => {'TELECOMUNICAZIONI': {'4C TEL': '26753g'}}
school_info => {'school_name': 'Test school 2', 'indirizzo': 'Address 2'}

Instead I would like to take only the names of the collections, without overloading the database by entering all the subcollections:

Test school 1
Test school 2

I want to clarify that I have already done some research on the web and I have not arrived at any solution for the admin sdk in Python. Thanks in advance

My structure:

Schools (collection) -> Lombardia (document) -> Milan (collection) -> Milan (document) -> Test School 1 (collection) -> 0 (document) -> various field

CodePudding user response:

In your sample code, Test School 1 and Test School 2 are subcollections inside the Milan document. If you need to fetch the names of all these subcollections from Milan, you can call the collections() method as you were doing, since this returns an iterator of all the collections in a specific reference location (in this case, the Milan document).

The iterator is of type CollectionReference, and this class has the convenient property of id, which simply returns the collection name without having to fetch any documents from the collections themselves. I quickly set up a test based on your structure to show this:

fire_db = firestore.client()

collections = fire_db.collection("Schools").document("Lombardia").collection("Milan").document("Milan").collections()

for collection in collections:
    print(collection.id) # Gets id property of each iterated collection reference

Output:

Test School 1
Test School 2

Let’s say instead you want to use the metadata document school_info to fetch the names from there. In that case, you can travel to that document by using each CollectionReference that is iterated over to get that specific document, and fetch the school_name from the document (produces the same output as above):

fire_db = firestore.client()

collections = fire_db.collection("Schools").document("Lombardia").collection("Milan").document("Milan").collections()

for collection in collections:
    print(collection.document("school_info").get().get("school_name")) # Fetches each school_info document, and then retrieves the school_name

CodePudding user response:

What you're trying to do is probably an indication that the data structure isn't really the best (so long term, its probably best to rethink it). But here's an article on this very topic. All of the solutions listed in the article are relatively hacky. But I don't think there's any other method at the moment.

  • Related