Firestore文档与Python的合并
我需要使用set()和{merge:true}将文档推送到Firebase Firestore数据库。我不能使用update(),因为我不能保证文档预先存在 如果我使用Angular&TypeScript,代码将是:Firestore文档与Python的合并,python,firebase,google-cloud-firestore,Python,Firebase,Google Cloud Firestore,我需要使用set()和{merge:true}将文档推送到Firebase Firestore数据库。我不能使用update(),因为我不能保证文档预先存在 如果我使用Angular&TypeScript,代码将是: import { AngularFirestore } from '@angular/fire/firestore'; constructor(private firestore: AngularFirestore) { } insertData() { this.fi
import { AngularFirestore } from '@angular/fire/firestore';
constructor(private firestore: AngularFirestore) {
}
insertData() {
this.firestore.collection('officePlants').doc('docXYZ')
.set({
'humidity': this.humiditySensorReading,
'temperature': this.temperatureSensorReading
},
{ merge: true }); // provides an update and creates the doc if it doesn't exist
}
我正试图用Python做同样的事情:
from google.cloud import firestore
import firebase_admin # sudo pip install firebase-admin
from firebase_admin import credentials
from firebase_admin import firestore
credentials_path = 'myServiceAccount.privateKey.json'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_path
cred = credentials.Certificate(credentials_path)
firebase_admin.initialize_app(cred)
db = firestore.Client()
doc_ref = db.collection(u'officePlants').document('docXYZ')
doc_ref.set({
u'humidity': humidity,
u'temperature': temperature
}, {'merge': True})
但我得到了一个错误:
ValueError(“无效的合并路径:{}”。格式(合并路径))
ValueError:无效的合并路径:FieldPath('合并')
删除引号时,{merge:True}
会出现错误
名称错误:未定义名称“合并”
谢谢 Python版本中的语法有点变化:
data = {
u'humidity': humidity,
u'temperature': temperature
}
doc_ref = db.collection(u'officePlants').document('docXYZ')
doc_ref.set(data, merge=True)