Firestore update only one field

Solution 1:

I know it's almost a year old question, but this might help someone. Use dot notation

db.collection("users")
  .document("frank")
  .update({
    "age": 13,
    "favorites.color": "Red"
  });

Solution 2:

When you call set() on a document, the existing contents of that documents are replaced with the data you pass in.

If you want to only update the values of the field you specify in a map, use update():

mFirebaseFirestore
  .collection("users")
  .document(mFirebaseAuth.getUid())
  .update(userMap)

See the Firestore documentation on updating a document.

Solution 3:

Please note that as of 2021, the syntax in MrAleister's answer has changed a bit if using firebase-firestore-ktx (see documentation). I did not edit answer because perhaps it is still valid for people using other versions of the library.

// Assume the document contains:
// {
//   name: "Frank",
//   favorites: { food: "Pizza", color: "Blue", subject: "recess" }
//   age: 12
// }
//
// To update age and favorite color:
db.collection("users").document("frank")
        .update(mapOf(
                "age" to 13,
                "favorites.color" to "Red"
        ))

Solution 4:

I'm finding some odd notation in some of these answers. Maybe firestore parameters were updated? At any rate, I wanted to post an answer of my own.

If you want to update a single element in your document, do so with the below code:

db.collection("users").document("frank")
  .update("age", 13);

The update() parameter accepts as many key/value pairs as you need, which are not separated by a colon, and are NOT put in some sort of array notation { } as shown in some of these answers. They are simply added in and separated by commas. See below:

.update( key , value , additionalkey, additionalvalue)

After update(), feel free to add your OnSuccess/OnFailure listeners as needed.