[英]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
.当每个医生(由他的 email 代表)都有一个名为patients_waiting
的集合时,我就有了 Doctor 集合。 Now, what I'm trying to do is delete one document from the paitents_waiting
collection by field calls patients containing his email .现在,我要做的是从包含他的email的字段调用患者中从paitents_waiting
集合中删除一个文档。
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.现在不好了,因为文档保存在 uid 中,而不是 email 但我尝试使用 where function 但没有成功。 how do I found this document by email and delete him?我如何找到 email 的这个文件并删除他?
I will mention that I'm doing it on flutter, but I think it doesn't matter.我会提到我是在 flutter 上做的,但我认为没关系。
as long as you have the patient's email address you can search with and delete it只要您有患者的 email 地址,您就可以搜索并删除它
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注意:您使用的是旧版本的 firestore
Inside your paitents_waiting
collection, the documents are NOT named according to patient email, they are randomly generated Firebase IDs.在您的paitents_waiting
集合中,文档不是根据患者 email 命名的,它们是随机生成的 Firebase ID。 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:如果您想遵循这种方法,否则应该可以工作,当您想创建患者等待文档时,请使用.set
而不是.add
,并将文档 ID 设置为您的专利的 email,如下所示:
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.要通过email
删除医生的所有患者,您可以结合使用 Firestore 查询和批量更新。 The first one we need to get all patients with the same email and the other to delete them.第一个我们需要获取所有具有相同 email 的患者,另一个删除它们。 A function for that would look like this:一个 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.