如何使用 Python 的 SDK 在 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'])

按照所选方法保存。

最新更新