Unable to delete document in flutter cloudfirestore? - flutter

I'm using the below to code to delete the doc, but the delete operation is not done in cloudfirestore databased,still the data exist after delete, Please guide.
Collection id:users,
document id: p0lncbJnslgsvtUHW7zcbauk62F3
My cloud firestore version: cloud_firestore: ^0.13.4+2
await Firestore.instance
.collection(collectionId)
.document(documentId)
.delete();

When you see a document ID in italics in the console, that document doesn't actually exist. It's shown because that document ID has subcollections nested under it (which you are not showing in your screenshot. If you want to delete those subcollections, you will need to query for and delete each document in those subcollections. There is no easy way around this in client applications. For server-side code, there are tools to delete a document and subcollections recursively.
See also:
Firestore documentation: Delete collections
Firestore DB - documents shown in italics
Parent document is being marked as "deleted" (italics) by default
Why are non auto-generated document Ids are in italics in Firestore console?

Related

Flutter Firebase delete a document of a subcollection using the document ID

Apparently, I was able to retrieve a subcollection data from firebase by using the code below
FirebaseFirestore.instance.collectionGroup('announcementlist').where('id', isEqualTo: '${docID}');
However, I am now trying to delete a subcollection by using this collectionGroup method and it is not working. I want all admin user to be able to delete the subcollections.
So what I found online is something similar to this line of code.
// define document location (Collection Name > Document Name > Collection Name >)
var docRef = Firebase.firestore().collection("Rooms").doc("bsYNIwEkjP237Ela6fUp").collection("Messages");
// delete the document
docRef.doc("lKjNIwEkjP537Ela6fhJ").delete();
which I got from codegrepper but as far as I know, the doc("bsYNIwEkjP237Ela6fUp") part requires the id of the user which is not the method I want because all admin users should be able to delete the data and it uses collection instead of collectiongroup
Is there any solution for this ? Please help.
FirebaseFirestore.instance.collection("Rooms").doc(snapshot.requireData.docs[i].id).collection("Messages").doc(snapshot.requireData.docs[i].id).delete();

how to automatically create field for subcollection firestore flutter

I have a subcollection of comments for the posts in a comments collection. However, how do I create a field (please, refer to the image) automatically?
I can create field manually from firestore console. But, I am not sure how to call it in the code. I cannot find it online. Without the field my subcollection document says:
This document does not exist, it will not appear in queries or snapshots
I use commentsRef().doc(widget.post.postId).collection('comments).set({data}) to create a comment, but how would I create a 'blahblah' field for the doc in sub-collection?
Thanks!
Your code is fine but you are trying to add field inside a collection which is not possible but just update your code to the following and it should work
commentsRef().doc(widget.post.postId).set({
'field': 'blahblah'
}).then((value) {
print('Document Updated');
});
Here is your result

how to delete a complete collection including sub-collection in firebase firestore

I am making an application in which i have to make invoice of cars and the pattern i am using to store the invoice is shown in image below
containerInvoice: {
invoiceNo:123,
total:$9000,
container:{
containerNo:911,
containerTotal:$201999
units:{
id:199,
chasis:a829nn,
}
}
}
if i directly delete containerInvoice's document then it is delete successfully but it leave it's sub-collection and sub-sub-collection undelete.
the way i fount to delete whole collection including its child collection is to access these documents by document's id and delete it, but it is a lengthy process.
Is there any way to delete a parent collection and all the child collection deleted automatically
As #danh pointed out, according to the documentation, when you delete a document, Cloud Firestore does not automatically delete the documents within its subcollections.
If you want to delete a document and all the documents within its subcollections, you must do so manually [1].
To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them [2].
[1] - https://firebase.google.com/docs/firestore/manage-data/delete-data#delete_documents
[2] - https://firebase.google.com/docs/firestore/manage-data/delete-data#collections

Update FireStore Document Id Using Flutter

I want to update firestore document Id Check This
First, you cannot update the id of a document in Firestore. You will have to copy your document with the new id and delete the old one.
But why do you want to do this, especially within the Users collection? You will lose the link between the Firebase Auth Users and the Users details in your Firestore database.
You can't update the document id directly. For that you first need to make a new document with your updated Id and then delete the previous one.
First of all you need to get the document and store the snapshot value in the variable document.
DocumentSnapshot snapshot= await Firestore.instance.collection('Users').document('yourId').get();
var document=snapshot.data;
After that you can
Firestore.instance.collection('Users').document('newId').setData(document);
Firestore.instance.collection('Users').document(document.id).delete();

How to add new field to already exist documents of the collection with cloud functions?

I have a collections called 'users' and I want to add new field called 'status: boolean' to already exists every documents using cloud functions and firestore database instantly.
There is no bulk update operation, so you will have to:
Query the collection for all documents
Iterate each one
Update the document with the new field