Reputation: 415
How to delete a document that has a subdocument in firestore database?
Here is the collection structure
I would like to delete the document doc("3F0SezA3")
I tried these below but none had work, i am getting console.log('deleted')
but in the database the document is still showing
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);
});
Upvotes: 1
Views: 425
Reputation: 83058
If you want to delete the two documents at the same time, the best is to use a batched write, as follows:
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)
});
Upvotes: 2