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

Categories

0 votes
1.5k 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 Reply

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...