【问题标题】:Update a document in firestore更新 Firestore 中的文档
【发布时间】:2023-04-05 00:46:02
【问题描述】:

我想在 Firebase 中更新一个文档。问题是只能更新特定字段?

我想做这样的事情:

fs.collection("users").document(user.id).update(user)

问题在于 Kotlin 强制将字段放入更新中,如下所示:

fs.collection("users").document(user.id).update("firstname", user)

但我不希望这样,我想用我的模型而不是字段来更新整个文档。

【问题讨论】:

  • 你使用的是什么节点版本?

标签: android firebase kotlin google-cloud-firestore


【解决方案1】:

使用以下代码行:

fs.collection("users").document(user.id).update("firstname", user)

您将只能更新一个属性。如果要更新多个属性,请使用以下代码行:

fs.collection("users").document(user.id).update("firstname", "John",
                                                "lastname", "Smith",
                                                "age", 25)

如果您想使用User 类的对象更新文档,请使用set() 方法而不是update(),如以下代码行所示:

fs.collection("users").document(user.id).set(user)

您还可以使用Map 更新文档,正如我在以下帖子中的回答中所述:

【讨论】:

    猜你喜欢
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    相关资源
    最近更新 更多