4

I would like the update a document in Firebase. The problem is that you can only update a specific field?

I would like to do something like this:

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

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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
stefan manson
  • 67
  • 1
  • 7

1 Answers1

15

Using the following line of code:

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)

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)

You can also use a Map to update a document, as explained in my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193