【问题标题】:Manipulating Google Cloud Firestore data in python在 python 中操作 Google Cloud Firestore 数据
【发布时间】:2020-02-02 20:26:03
【问题描述】:

我正在尝试使用 python 读取 Google Cloud Firestore 中的特定数据字段。

我的 Firestore 数据库中有一个名为 Products 的集合,并且手动添加了多个具有不同字段的文档。

到目前为止,我可以使用以下方法提取文档:

docs = db.collection(u'Products').where(u'checked', u'==', False).stream()
for doc in docs:
        print(u'{} => {}'.format(doc.id, doc.to_dict()))

这可行,我收到以下输出:

Test2 => {'link': 'https://stackoverflow.com', 'price': 14, 'checked': False}

但是,我无法从该字典中提取单独的“链接”字符串。我试过了:

print(doc.to_dict('link'))

并多次迭代,得到以下输出:

to_dict() 接受 1 个位置参数,但给出了 2 个

我一直在关注 firebase 文档 here,但没有找到专门打印字段的示例。

关于如何从我使用的查询中打印“链接”字符串的任何建议?

我正在运行 Python 3.7。

提前感谢您的帮助。

【问题讨论】:

    标签: python firebase dictionary google-cloud-firestore


    【解决方案1】:

    to_dict 返回一个字典,因此您的代码应该如下所示:

    print(doc.to_dict()['link'])
    

    而不是直接传递参数。

    或者,因为您只需要一个可以尝试的字段:

    print(doc.get('link'))
    

    因为它避免创建整个快照的副本。

    【讨论】:

    • 非常感谢 robsiemb,print(doc.to_dict()['link']) 工作得很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 2020-02-07
    • 2021-09-03
    相关资源
    最近更新 更多