Reputation:
Is it possible in Firestore to delete some documents, where the "Value Name" is the same?
For example: I have some UID's as Documents inside a Collection. Inside these Documents will be saved two types of "Value Names". 1st "Value Name" is called "byCar". 2nd "Value Name" is called "byFoot". Now I want to delete all Documents, where the "Value Name" is equal to "byCar". All other documents, where the "Value Name" is "byFoot" will be untouched. Is something like this possible?
I program in Flutter / Dart and it would be awesome, if someone could provide me an answer, because I was not able to find somthing on the internet.
Upvotes: 0
Views: 566
Reputation: 378
Your code would look something like this:
QuerySnapshot querySnapshot = await db
.collection("yourPathCollection")
.where("Value Name", isEqualTo: "byCar")
.get()
.then((querySnapshot) {
querySnapshot.docs.forEach((doc) {
doc.reference.delete();
});
return null; });
Perhaps Cloud Firestore will block your request, so you will need to change your database rules, according to your need.
Upvotes: 0
Reputation: 599101
Firestore doesn't support update queries, where you send a query to the server and it updates (or in your case deletes) all matching documents. To write or delete a document you will need to know its entire path in your application code.
So that means you need to perform two steps to delete the documents:
In code that'd be something like:
refUser.where("city", isEqualTo: "CA").getDocuments().then((querySnapshot){
for (DocumentSnapshot documentSnapshot in querySnapshot.documents){
documentSnapshot.reference.delete();
});
snapshot.documents.first.reference.delete();
});
Also see:
Upvotes: 2