[英]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 1
和Test School 2
是Milan
文档中的子集合。 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.