Firestore Partial Update Nested Field or Map

May 18, 2019

Partial update Nested Field

Success

docRef.update(mapOf(
    "person.name" to "Desmond"
))

or

docRef.set(mapOf(
    "person" to mapOf(
        "name" to "Desmond"
    )
), SetOptions.merge())

NOTE: Firestore Partial Update: Create Document if Not Exis.

Fail

This replace person with name: Desmond (replace/delete all existing entries in person).

docRef.update(mapOf(
    "person" to mapOf(
        "name" to "Desmond"
    )
))

This create a new field person.name instead of adding name to person.

docRef.set(mapOf(
    "person.name" to "Desmond"
), SetOptions.merge())

Partial update Map

Nested Field is actually a map, so its behaviour is exactly the same as updating a map.

docRef.update(mapOf(
    "maps.3" to true
))

or

docRef.set(mapOf(
    "maps" to mapOf(
        "3" to true
    )
), SetOptions.merge())

Fail

This replace maps with 3: true (replace/delete all existing entries in maps).

docRef.update(mapOf(
    "maps" to mapOf(
        "3" to true
    )
))

This create a new field maps.6: true instead of adding 6 to maps.

docRef.set(mapOf(
    "maps.6" to true
), SetOptions.merge())

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.