python - Cloud Firestore - 将数据附加到子集合

问题描述 投票:1回答:1

是否可以使用python firebase admin sdk将数据附加到云firestore数据库中的子集合?如果是这样,我错过了什么?

我试图将数据附加到位于googles cloud firestore中的特定文档的子集合中 - 而不是实时数据库。我做了大量的研究,这是迄今为止最普遍的资源:add data to firestore没有明确表示不可能

我能够创建文档,阅读文档等。我似乎无法附加或访问子集合而不获取整个文档

我的代码流程如下:

import time
import json
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
from firebase_admin import firestore

#authentication
cred = credentials.Certificate('key.json')
app = firebase_admin.initialize_app(cred)

cloudDb = firestore.client()    #open connection. uses 'app' implicitly
doc = cloudDb.collection(collection).document(document)
data['date'] = firestore.SERVER_TIMESTAMP
data['payload'] = [0,1,2,3,4,5]
doc.collection("data").set(data)   #*

#--testing (collection)
# |
#----docId  (document)
#   |
#------company (field)
#------accountinfo(field)
#------data (subcollection)
#     |
#--------date (field)
#--------payload (array)

然而,这在最后一行(带星号的行)上失败并出现错误:

('Cannot convert to a Firestore Value', <object object at 0xb6b07e60>, 'Invalid type', <class 'object'>)
python-3.x google-cloud-firestore
1个回答
0
投票

好吧,所以解决了我自己的问题,感谢Doug让我写一个可以在另一台机器上运行的脚本。 (即较小的脚本少进行)我的问题是尝试将集合设置为我的数据对象,而不是在集合中创建文档并设置它。即:

doc.collection("data").set(data)  #error

doc.collection("data").document().set(data)  #success
© www.soinside.com 2019 - 2024. All rights reserved.