在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屋!