简体   繁体   English

如何获取文档中集合的名称

[英]How to get the name of the collection in the document

After various researches and tests I decided to write here.经过各种研究和测试,我决定写在这里。 I am building an 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.我正在 Python 中构建一个 API,我正在使用 Firebase-Admin SDK。在 Firestore 上,我有一个包含许多 collections 和子集合的数据库,通过此代码,我可以获取最终集合中的文档。 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.具体来说,我想获取文档中存在的 collections 的所有名称。

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:相反,我只想获取 collections 的名称,而不会因输入所有子集合而使数据库过载:

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我想澄清一下,我已经对 web 进行了一些研究,但我还没有为 Python 中的管理员 SDK 找到任何解决方案。提前致谢

My structure:我的结构:

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

In your sample code, Test School 1 and Test School 2 are subcollections inside the Milan document.在您的示例代码中, Test School 1Test School 2Milan文档中的子集合。 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).如果您需要从Milan获取所有这些子集合的名称,您可以像之前那样调用collections()方法,因为这会返回特定引用位置(在本例中为 Milan 文档)中所有 collections 的迭代器.

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.迭代器是CollectionReference类型,这个 class 有一个方便的属性id ,它只返回集合名称,而不必从 collections 本身获取任何文档。 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: 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.假设您想使用元数据文档school_info从那里获取姓名。 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):在这种情况下,您可以使用迭代的每个CollectionReference来访问该文档以获取该特定文档,并从文档中获取school_name (生成与上述相同的 output):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 firebase 文档中获取集合名称 - Get collection name from firebase document 遍历文档时如何获取集合中文档的文档引用 - how to get the document reference of a document in a collection when iterating through the documents Firestore - 如何在将文档添加到集合后获取文档 ID - Firestore - How to get document id after adding a document to a collection flutter firebase 文档下如何打印藏品名称 - How to print collection name under document in flutter firebase 如何使用模块 Web SDK 的版本 9 在 Cloud Firestore 集合中的文档中获取文档和嵌套集合? - How do I get document and nested collection in a document in a Cloud Firestore collection using Version 9 of the Modular Web SDK? 在 firebase 上获取父集合和子集合文档 - Get parent collection & sub collection document on firebase 在 Cloud Firestore 事务中,如果我们不知道文档名称,如何检查文档是否存在于特定集合中 - in Cloud Firestore transaction how Check to see if a Document exists in a specific Collection if we don't know Document name 如何获取集合 Cloud Firestore 中的文档 ID 列表? - How to get a list of document IDs in a collection Cloud Firestore? 如何使用angular firestore获取集合中的文档列表 - how to get list of document in a collection, using angular firestore Flutter Firestore 更新集合内的文档名称 - Flutter Firestore update document name inside of collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM