如何通过获取自动生成的 ID 从 Firestore 中删除文档?

huangapple go评论107阅读模式
英文:

How to delete a document in firestore by fetching its autogenerated id?

问题

如何在Firestore中删除文档或子文档,以及如何获取其自动生成的ID,以便当用户长按列表中的行项目并选择“删除”时,可以轻松地从应用程序的用户界面中删除文档。

英文:

How to delete a document or a sub-document in Firestore & how to fetch its auto-generated id so that when the user long presses, selects 'delete' on a row item in the list so that he/she can delete a document from the user interface of the app easily.

答案1

得分: 2

你无法“获取”现有的随机文档。为了删除文档,您需要执行以下两种操作之一:

  • 在客户端上记住生成的ID,并使用该ID构建一个DocumentReference以删除该文档。
  • 使用您在文档中已知的字段查询文档,然后在查询之后删除它。或者简单地查询所有文档,并将它们作为一组进行处理。

如果您无法使用其字段查询文档,也不知道其ID,那么您就陷入了困境,您需要更仔细地考虑数据模型。

英文:

You can't "fetch" an existing random document. In order to delete a document you need to do either one of two things:

  • Remember the generated ID on the client, and use that to build a DocumetnReference to delete the document
  • Query for the document using a field that you know in that document, then delete it after the query. Or simply query for all documents and work with them as a group.

If you can't query for a document using its fields, and you don't know it's ID, you're kind of stuck, and you will need to think more carefully about your data model.

答案2

得分: 1

如何在Firestore中删除文档或子文档

要删除文档,您需要使用 delete() 方法。

Kotlin 代码

db.collection("你的集合名称").document("文档ID")
    .delete()
    .addOnSuccessListener { Log.d(TAG, "文档快照已成功删除!") }
    .addOnFailureListener { e -> Log.w(TAG, "删除文档时出错", e) }

您需要知道要删除的文档的 集合名称文档ID。使用您的 集合名称文档ID 来从集合中删除文档。

查看这个链接了解更多信息。

如何获取自动生成的文档ID

要读取集合中的所有文档,您还需要知道 集合名称

在 Kotlin 中读取集合中的文档

db.collection("你的集合名称")
    .get()
    .addOnSuccessListener { result ->
        for (document in result) {
            Log.d(TAG, "${document.id} => ${document.data}")
        }
    }
    .addOnFailureListener { exception ->
        Log.d(TAG, "获取文档时出错:", exception)
    }

document.id 将给您每个 文档ID。使用这个 文档ID 来删除文档。

要从集合中读取所有文档,请查看这个链接

英文:

> How to delete a document or a sub-document in Firestore

To delete document you have to use delete() method

Kotlin Code:

db.collection("your_collection_name").document("documentId")
        .delete()
        .addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully deleted!") }
        .addOnFailureListener { e -> Log.w(TAG, "Error deleting document", e) }

You need to know your collection name and documentId you want to delete. Use your collection name and documentId to delete a document from a collection.

Check this for more

> how to fetch its auto-generated id

To read all document in a collection you also need to know collection name.

To read documents in a collection in Kotlin:

db.collection("your_collection_name")
        .get()
        .addOnSuccessListener { result ->
            for (document in result) {
                Log.d(TAG, "${document.id} => ${document.data}")
            }
        }
        .addOnFailureListener { exception ->
            Log.d(TAG, "Error getting documents: ", exception)
        }

document.id will give you every documentId. Use this documentId to delete a document.

To read all documents from a collection check this

huangapple
  • 本文由 发表于 2020年9月20日 03:22:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63972600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定