[英]How to do a Batch delete Firestore Documents in a Firebase cloud function
[英]How to delete documents in a collection with the same key value in Firebase Cloud Firestore
我有一个集合,集合中的每个文档都有一个名为“group_id”的键。 我想删除所有分配给 group_id 的具有相同值的文档。 我该怎么做呢?
以下是其中一份文件的屏幕截图。 注意图像中的 group_id 键。
我可以删除单个文档并遍历 collections,但是我无法将这两件事结合起来迭代和删除我想要删除的文档。
删除单个文档
export async function deleteCalendarEventFromFirebase(authenticatedUsersUid, id) {
const snapshot = await firebase.firestore().collection('users').doc(authenticatedUsersUid).collection('calendar_events')
return snapshot.doc(id).delete();
}
迭代并获得一个集合
export async function getClientCalendarEventsFromFirebase(authenticatedUsersUid,clientID) {
const snapshot = await firebase.firestore().collection('users').doc(authenticatedUsersUid).collection('calendar_events').where("client_id", "==", clientID).get();
return snapshot.docs.map((val) => {
let dataObj = val.data()
dataObj.id = val.id;
return dataObj
})
}
谢谢你。
您需要从每个文档的删除中收集承诺,使用 Promise.all() 将其转换为单个 promise,并确保最终的 promise 直到所有文档都被删除后才解决。 它有助于了解DocumentSnapshot中的所有内容。
export async function yourFunction(authenticatedUsersUid,clientID) {
const snapshot = await firebase.firestore()
.collection('users')
.doc(authenticatedUsersUid)
.collection('calendar_events')
.where("client_id", "==", clientID)
.get();
await Promise.all(snapshot.docs.map(doc => doc.ref.delete()));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.