简体   繁体   English

如何从 Firestore 获取所有文档的特定字段?

[英]How to get specific fields for all documents from Firestore?

The following code returns skills field from GOOGL103 document only.以下代码仅从 GOOGL103 文档返回技能字段。

However I want to get skills field from all the documents in job collection, how do I do that?但是我想从工作集合中的所有文档中获取技能字段,我该怎么做?

import firebase_admin
from firebase_admin import credentials,firestore

cred = credentials.Certificate("service_key.json")
firebase_admin.initialize_app(cred)

job_keywords = firestore.client().collection(u'job').document('GOOGL103').get().to_dict()['skills']

print(f'Skills Required: {job_keywords}')

You could just query the collection and loop all the documents to get the specific fields.您可以只查询集合并循环所有文档以获取特定字段。 Check the code i wrote below:检查我在下面写的代码:

docs = db.collection(u'job').stream()

for doc in docs:
    skills = u'{}'.format(doc.to_dict()['skills'])
    print(skills)

There are more sample snippets on Firestore documentation that you can use for your other use-cases. Firestore 文档中有更多示例片段,您可以将其用于其他用例。 You may refer here for more information.您可以参考此处了解更多信息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM