python - 如何使用Python的SDK在Firestore上编写位置、引用和时间戳数据类型?

标签 python python-3.x firebase nosql google-cloud-firestore

文档甚至解释了如何编写以下数据类型:

data = {
  u'stringExample': u'Hello, World!',
  u'booleanExample': True,
  u'numberExample': 3.14159265,
  u'dateExample': datetime.datetime.now(),
  u'arrayExample': [5, True, u'hello'],
  u'nullExample': None,
  u'objectExample': {
    u'a': 5,
    u'b': True
  }
}

我正在将 Mongo DB 集合迁移到 FireStore。

这是我到目前为止的代码:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

def initialize_firestore():
  cred = credentials.Certificate('admin.json')
  app = firebase_admin.initialize_app(cred)
  db = firestore.client()
  return db

db.collection('My_Collection').document('desired_iD').set('Document_to_write')

我想编写一个包含引用和位置字段以及自定义日期的文档。

最佳答案

获取响应(res)并初始化字典(Document_to_write)后:

Document_to_write['date_type'] = datetime.datetime.strptime(res['created'], "date format in string")
Document_to_write['reference_type'] = db.document(u'Collection_Name/'+res['collection_id'])
Document_to_write['location_type'] = firestore.GeoPoint(res['latitude'], res['longitude'])

按照选择的方法保存。

关于python - 如何使用Python的SDK在Firestore上编写位置、引用和时间戳数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50674271/

相关文章:

python - 如何更改 jupyter favicon.ico

python - 我可以使用 pop() 函数来传递用户输入吗?

python - 从 pymongo 映射缩减操作记录/打印

python - 为什么不同的格式方法在 Python 中表现不同?

javascript - 如何在不使用验证码验证器的情况下实现Firebase电话验证系统?

Firebase 部署 : Error: Could not detect language for functions at

python - 仅从数据框中获取前三个值

Python3 和 hmac 。如何处理不是二进制的字符串

python - 与无关(忽略)字符的字符串比较

java - 错误 : Failed to resolve: com. google.firebase :firebase-core:18. 1.0 在项目结构对话框中显示受影响的模块:app