1
votes

Is it possible to append data to a subcollection in the cloud firestore database using the python firebase admin sdk? If so, what am i missing?

I am trying to append data into a subcollection of a specific document located in googles cloud firestore - Not the real time database. I have done a fair amount of research with this being the most prevalent resource so far: add data to firestore which does not explicitly say it isn't possible

i am able to create documents, read documents, etc. i just cannot seem to append to or access subcollections without getting the whole document

the flow of my code goes as follows:

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)

this however fails on the last line (line with asterisk) with the error:

('Cannot convert to a Firestore Value', <object object at 0xb6b07e60>, 'Invalid type', <class 'object'>)
1
Please edit the question to show exactly what data is. That's the problem here. This has nothing to do with the fact that you're adding to a subcollection rather than a collection. All collections and subcollections have the same API. - Doug Stevenson
Hi Doug, ive updated the question to show what data is. it is a dict object, which i can pass through successfully to the firestore api as a document, but i am unable to pass through the same dict obj into a document of a subcollection - Tokens94
Please show the entire, exact contents of data. Minimally, someone should be able to run your exact code to reproduce the problem. Also, you should state the expected result of the code. What exactly do you think the document should look like afterward? - Doug Stevenson

1 Answers

2
votes

ok so solved my own question thanks to Doug getting me to write a script which could be run on another machine. (ie a smaller script with less going on) my issue was trying to set the collection as my data object instead of creating a document within the collection and setting that. ie:

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

doc.collection("data").document().set(data)  #success