【发布时间】:2019-04-28 16:37:29
【问题描述】:
我正在尝试更新 PITA 非规范化数据库结构。 我知道已经有关于如何检查文档是否存在于 get 上的答案,甚至文档都非常清楚,但我找不到任何可以检查文档是否存在于更新“set”和“where”上的东西。
首先我想在更新前检查一个文档是否存在
const staffRef = db.collection("staff").doc(uid)
return staffRef.set({
employeeProfile: employeeProfile
}, {
merge: true
})...
有什么方法可以检查该文档是否存在于片场,或者我应该先阅读它以了解该文档是否像这样存在
const staffRef = db.collection("staff").doc(uid)
return staffRef.get()
.then((doc) => {
if (doc.exists) {
return staffRef.set({
employeeProfile: employeeProfile
}, {...
第二我要检查多个文档的位置
const staffRef = db.collection("staff").where("employerId", "==", uid)
const batch = db.batch()
return staffRef.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
batch.update(doc.ref, { employerProfile: employerProfile })
})...
如果存在,我应该在 forEach 之后阅读每个文档吗?
【问题讨论】:
标签: javascript firebase google-cloud-firestore google-cloud-functions