簡體   English   中英

如何在firestore上的集合內的文檔內的集合內按字段刪除文檔

[英]how to delete document by field inside collection inside document inside collection on firestore

當每個醫生(由他的 email 代表)都有一個名為patients_waiting的集合時,我就有了 Doctor 集合。 現在,我要做的是從包含他的email的字段調用患者中從paitents_waiting集合中刪除一個文檔。

圖 1

圖 2

但我嘗試了很多解決方案,但現在沒有一個對我有用。 我試圖做的事情:

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

現在不好了,因為文檔保存在 uid 中,而不是 email 但我嘗試使用 where function 但沒有成功。 我如何找到 email 的這個文件並刪除他?

我會提到我是在 flutter 上做的,但我認為沒關系。

只要您有患者的 email 地址,您就可以搜索並刪除它

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

注意:您使用的是舊版本的 firestore

在您的paitents_waiting集合中,文檔不是根據患者 email 命名的,它們是隨機生成的 Firebase ID。 細看。

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

如果您想遵循這種方法,否則應該可以工作,當您想創建患者等待文檔時,請使用.set而不是.add ,並將文檔 ID 設置為您的專利的 email,如下所示:

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

這應該讓事情為你工作。

要通過email刪除醫生的所有患者,您可以結合使用 Firestore 查詢和批量更新。 第一個我們需要獲取所有具有相同 email 的患者,另一個刪除它們。 一個 function 看起來像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM