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

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();

Related

Is there a way to move from one firebase collection to another collection including its document in flutter?

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.

how to use firestore wherefield method and adding document that collection to another collection

I am working a project to use shops. My structure data:
My code snippet
//current user sign in here and uid "VjZfKF1zmrV00C9VeTZxRmogybA2"
let currentUser = Auth.auth().currentUser
let db = Firestore.firestore()
db.collection("Blah").whereField("users", isEqualTo: currentUser?.uid).collection("otherPath").document().addDocument
I want to add data to use currentuser uid and if it is matching "Blah" inside documents then add that targeting document other collection.
.adddocumets or .setData it isn't allowed to firestore, so how can i figure it?
There is no way to send a query and an update statement to Firestore in one go. You will instead have to:
Execute a query to determine all documents that need to be updated.
Loop over the results of that query in your application code.
Update each document in turn inside that loop.
In addition, since users is an array field, you'll want to use the array-contains operator to match the correct documents.

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

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 can I segregate user data in firebase?

I'm creating an app that has uses a firebase Cloud Firestore database. The structure seems to be collection/document/fields. I am thinking of either using the user id as the prefix to the collection name or simply a field for userId.
I'm currently using:
Firestore.firestore().collection("Events").
This could be changed to prefix event with the userId
I am currently reading everything in using:
reference(to: "Events").addSnapshotListener{ (snapshot, _) in
guard let snapshot = snapshot else {return}
for document in snapshot.documents {
// code here
}
}
It's not a really good idea to use prefixes on collection names. That doesn't work well with security rules.
The usual structure for per-user data is to have a collection with documents whose IDs are the user IDs. Then, you can further organize other data in subcollections under that document ID.
/users
/uid1
/uid2
/events
/likes
/history
Then, you can write security rules using the user's UID very easily.