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:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.