如何删除Firestore数据库中具有子文档的文档?

时间:2020-06-14 05:10:11

标签: javascript firebase google-cloud-firestore

如何删除Firestore数据库中具有子文档的文档?

这是集合结构

enter image description here enter image description here 我想删除文档doc("3F0SezA3")

我在下面尝试了这些方法,但没有任何工作,我得到了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);
              });

1 个答案:

答案 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)
});