更新 firestore 中的文档 [英] Update a document in firestore
问题描述
我想更新 Firebase 中的文档.问题是你只能更新一个特定的字段?
I would like the update a document in Firebase. The problem is that you can only update a specific field?
我想做这样的事情:
fs.collection("users").document(user.id).update(user)
问题在于 Kotlin 强制将字段放入更新中,如下所示:
The problem is that Kotlin forces to put a field in update, like this:
fs.collection("users").document(user.id).update("firstname", user)
但我不想那样,我想用我的模型而不是字段来更新整个文档.
But I don't want that, I want to update the whole document with my model not a field.
推荐答案
使用以下代码行:
fs.collection("users").document(user.id).update("firstname", user)
您将只能更新一个属性.如果要更新多个属性,请使用以下代码行:
You'll be able to update only a single property. If you want to update multiple properties, please use the following lines of code:
fs.collection("users").document(user.id).update("firstname", "John",
"lastname", "Smith",
"age", 25)
如果您想使用 User
类的对象更新文档,请使用 set()
方法而不是 update()
>,就像下面这行代码:
If you want to update a document using an object of your User
class, please use the set()
method instead of update()
, like in the following line of code:
fs.collection("users").document(user.id).set(user)
您还可以使用 Map
来更新文档,如我在以下帖子中的回答中所述:
You can also use a Map
to update a document, as explained in my answer from the following post:
这篇关于更新 firestore 中的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!