简体   繁体   English

如何在firestore上的集合内的文档内的集合内按字段删除文档

[英]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集合中删除一个文档。

图 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.现在不好了,因为文档保存在 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.

相关问题 如何使用 Flutter 在 Firestore 中的文档内创建集合? - How to create a collection inside a document in Firestore with Flutter? Flutter Firestore 更新集合内的文档名称 - Flutter Firestore update document name inside of collection 如何在文档中创建集合? - How to create a collection inside a document? 如何使用 Flutter 在 Firestore 的 uid 文档中创建集合? - How to create a collection inside a uid document in Firestore using Flutter? 如何访问firestore文档中的集合并将其作为列表分配给我的model中的dart列表? - how to access a collection inside a firestore document and assign it as a list to my dart list inside my model? 获取我将在 firestore 的集合中显示的文档列表 - Get list of document I’d present inside a collection in firestore 如何从 Firebase Firestore 集合中指定文档的特定字段获取数据并将其显示到我的 UI? - How do I get data from a specific field of the specified document inside a Firebase Firestore collection and display it to my UI? 如何在不获取 Firestore 中文档内容的情况下获取集合中文档 ID 的列表? - How do I get list of the document ID's inside a collection without getting the content of the documents in Firestore? 如何使用表单验证来使用Flutter验证集合Firestore中是否存在文档 - How to use form validation to verify that document exists inside collection Firestore with Flutter 如何删除 firestore 集合中的当前用户文档? - How to delete current user document in firestore collection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM