且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过自动生成的文档ID从Firestore中删除文档

更新时间:2023-09-23 17:52:10

要使用要查找的文档ID,首先需要将其存储在变量中.当您将文档添加到数据库中并且使用对 document()方法的调用而没有传递参数时,将生成唯一的ID.要获取该ID,您应该使用以下代码:

In order to use the document id that you are looking for, first you need to store it in a variable. When you are adding a document to the database and you are using a call to document() method without passing an argument, a unique id is generated. To get that id, you should use the following code:

String documentId = postsRef.document().getId();
yourRef.document(documentId).set(yourModelObject);

其中 postsRef Posts 集合的 CollectionReference 对象,而 yourModelObject Post 类.我还建议您将该ID存储为Post文档的属性.

In which postsRef is the CollectionReference object of your Posts collection and yourModelObject is the object of your Post class. I also recommend you store that id, as a property of your Post document.

一旦有了这个ID,就可以像这样使用:

Once you have this id, you can use in your refence like this:

firebaseFirestore
    .collection("Posts")
    .document(documentId)
    .delete().addOnSuccessListener(/* ... */);