【问题标题】:Search by value to all Firestore collection and subcollections with admin-sdk in Python在 Python 中使用 admin-sdk 按值搜索所有 Firestore 集合和子集合
【发布时间】:2022-07-12 17:03:48
【问题描述】:

我正在制作一些 API,其中很多东西能够通过值检索相应的文档。 我的数据结构如下

Schools (collection) -> Lombardy (doc) -> Milan (collection) -> Milan (doc) - School Test1 (collection) -> 0 (doc) -> document

在学校文件中有以下信息:

address: School address 1
INFORMATICS {
    4A INF: "GA24HJ"
}

我的目标是使用代码 GA24HJ 检索学校名称 (School Test1) 和 address。 我的代码:

firestore = firestore.client()

collections = firestore.collections()
        for doc in collections.stream():
            print(f'{doc.id} => {doc.to_dict()}')

结果:

Lombardy => {}

我想澄清一下,我没有在网上找到任何有用的东西,我正在使用 Python 和 admin sdk。

【问题讨论】:

    标签: python database api google-cloud-firestore google-admin-sdk


    【解决方案1】:

    根据您共享的文档结构,没有“学校名称”。假设这是一个错误,而您实际上有这样的事情,

    {
        "name": "School Test1"
        "address": "School address 1"
        "INFORMATICS": {
            "4A INF": "GA24HJ"
        }
    }
    

    您可以使用点表示法通过地图字段中的嵌套字段进行查询:

    firestore
    .collection('Schools')
    .where('INFORMATICS.4A INF', '==', 'GA24HJ')
    .stream()
    

    请注意,使用 CollectionRef stream() 优于 get()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 2018-04-03
      • 2019-03-08
      相关资源
      最近更新 更多