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();
Related
I'm working with a document and I want to create an empty collection inside the document.
FirebaseFirestore.instance.collection('Users').doc("thisdoc). ??? (create collection inside this document)
I don't know what to add instead of ???
The documentation will help you there.
NOTE The Firebase team is migrating these docs onto firebase.com directly, so this link may get old quickly in the next months.
EDIT. Since you need to create it under a subcollection, simply:
Take a reference of that document
Reference a subcollection in that document, even if it doesn't exist, yet
Create a first document in there, and your subcollection is made.
Pseudocode:
// WARN, PSEUDOCODE AHEAD
final documentRef = firestore.collection("myCol").document("myDoc");
final subCollectRef = documentRef.collection("mySubCol");
subCollectRef.add({
'your': 'data',
});
im working on e-commerce project, im trying to move data in 'cart' collection into 'orders' collection once the user complete with payment so that i can create order history. previously i have no problem moving single data in firestore but since orders can have multiple item, how do i move it all to another collection?
here is the way i found but its not working.
CollectionReference copyFrom = FirebaseFirestore.instance.collection('users').doc(user!.uid).collection('cart');
CollectionReference copyTo = FirebaseFirestore.instance.collection('users').doc(user!.uid).collection('orders');
copyFrom.get().then((value) => {
copyTo.add(value)
});
There is no move operation in the Firestore API, so you'll have to read each document, then write it to its new location, and delete it from its original location. You might want to use a transaction for this.
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();
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
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