如何删除Firestore数据库中具有子文档的文档?
这是集合结构
我在下面尝试了这些方法,但没有任何工作,我得到了console.log('deleted')
,但是在数据库中文档仍在显示
firebase.firestore()
.collection("users")
.doc("3F0SezA3").delete()
.then(function () {
console.log('deleted');
}, function (error) {
console.error('Delete Error', error);
});
firebase.firestore()
.collection("users").doc("3F0SezA3")
.collection("movives").doc("asdrftgyqweAsdghj120a").delete()
.then(function () {
console.log('deleted');
}, function (error) {
console.error('Delete Error', error);
});
答案 0 :(得分:2)
如果要同时删除两个文档,最好使用batched write,如下所示:
var batch = firebase.firestore().batch();
var parentRef = firebase.firestore()
.collection("users")
.doc("3F0SezA3");
batch.delete(parentRef);
var childRef = parentRef
.collection("movives")
.doc("asdrftgyqweAsdghj120a");
batch.delete(childRef);
batch.commit()
.then(function () {
console.log("Docs deleted")
})
.catch(function (error) {
console.log(error)
});