简体   繁体   中英

how to delete document by field inside collection inside document inside collection on firestore

I have Doctor collection when each doctor (represent by his email ) has a collection called patients_waiting . Now, what I'm trying to do is delete one document from the paitents_waiting collection by field calls patients containing his email .

图 1

图 2

but I tried many solutions and none of them works for me now. what I have tried to do:

Firestore.instance
       .collection("Doctors")
       .document(doctorEmail)
       .collection("paitents_waiting")
       .document(paitentEmail)
       .delete();

now it's not good because the document is saved in uid and not by email but I tried to play with the where function but with no success. how do I found this document by email and delete him?

I will mention that I'm doing it on flutter, but I think it doesn't matter.

as long as you have the patient's email address you can search with and delete it

 Firestore.instance
            .collection("Doctors")
            . document(doctorEmail)
            .collection("paitents_waiting")
            .where('patient', isEqualTo:paitentEmail )
            .get().then((value) => value.docs.single.reference.delete())

Nb: you are using an old version of firestore

Inside your paitents_waiting collection, the documents are NOT named according to patient email, they are randomly generated Firebase IDs. Take a closer look.

Firestore.instance
       .collection("Doctors")
       .document(doctorEmail)
       .collection("paitents_waiting")
       .document(paitentEmail) //this in your Firebase isn't an email, it's "xaErf43Asd..etc"
       .delete();

If you want to follow this approach, which should be working otherwise, when you want to create a patient waiting document, use .set instead of .add , and set the document id to your pateint's email, like this:

Firestore.instance
       .collection("Doctors")
       .document(doctorEmail)
       .collection("paitents_waiting")
       .document(paitentEmail)
       .set({your patient data here});

This should get things working for you.

To delete all patients for a doctor by the email you can use a combination of Firestore query and batch updates. The first one we need to get all patients with the same email and the other to delete them. A function for that would look like this:

Future<void> deletePatiensForDoctor(String docEmail, String patEmail) async {
  WriteBatch batch = FirebaseFirestore.instance.batch();

  QuerySnapshot querySnapshot = await Firestore.instance
      .collection("Doctors")
      .document(docEmail)
      .collection("paitents_waiting")
      .where('email', isEqualTo: patEmail)
      .get();

  querySnapshot.docs.forEach((doc) {
    batch.delete(doc.reference);
  });

  return batch.commit();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM