【问题标题】:Read Data | Cloud Firestore and Python读取数据 | Cloud Firestore 和 Python
【发布时间】:2022-01-04 12:37:33
【问题描述】:

我在Firestore 中有一个commodity_price 表,具有以下结构-

collection/document/collection/document/field

以下是所包含信息的类型-

commodity_price(表格)/city/commodity/date/price

问题-

  1. 如何获取表中所有城市的列表?
  2. 如何获取包含特定日期数据的城市列表?

我尝试使用以下代码但返回 0 条记录 -

docs = db.collection('commodity_price').get() 
for doc in docs:
    print(doc.to_dict())

【问题讨论】:

    标签: python google-cloud-firestore


    【解决方案1】:

    对于您的第一个问题,关于如何在您的 commodity_price 文档中获取所有 cities 的列表,我发现 this similar question 有一个可接受的答案,我认为您可以根据您的用例进行调整。比如:

    docs = db.collection('commodity_price') 
    results = docs.stream()
    for result in results:
        city = u'{}'.format(doc.to_dict()['city'])
        print(city)
    
    
    

    解决您的第二个问题,关于如何获取按特定日期过滤的cities 列表。我认为您可以使用与以前类似的方法,但包括您想要的日期的过滤器,例如:

    date = "2022-01-01"
    
    docs = db.collection('commodity_price') 
    docs.where("date", "==", date)
    results = docs.stream()
    for result in results:
        city = u'{}'.format(doc.to_dict()['city'])
        print(city)
    

    最后一个解决方案基于this answerthis documentation

    【讨论】:

      猜你喜欢
      • 2018-10-26
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      相关资源
      最近更新 更多