Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

firebase - How to delete Documents by Value name?

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.

question from:https://stackoverflow.com/questions/65862073/how-to-delete-documents-by-value-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

  1. Execute a query to find all documents matching your condition.
  2. Loop over the results and delete them.

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...