Firestore Partial Update: Create Document if Not Exist

May 18, 2019

To perform partial update on new/existing document, use set with SetOptions.merge()

    "name" to "Partial replace"
), SetOptions.merge())

Create a kotlin extension for updateOrCreate.

fun DocumentReference.updateOrCreate(data: Any): Task<Void> {
    return set(data, SetOptions.merge())

NOTE: Beware there are some bahavior difference between set with SetOptions.merge() vs update for update map and updated nested field.

Update Fail if document does not exist.

Use update for partial update.

    "name" to "Partial replace"

The update will fail if applied to a document that does not exist.

with the following error.

java.util.concurrent.ExecutionException: NOT_FOUND: No document to update
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.