6

I need to update a field in a nested object with a dynamic key. the path could look like this: level1.level2.DYNAMIC_KEY : updatedValue The update-method deletes everything else on level1 instead of only updating the field in the nested object. The update() acts more like a set(). What am I doing wrong?

I tried the following already:

I read the documentation https://firebase.google.com/docs/firestore/manage-data/add-data#update-data but that way it is a) static and b) still deletes the other fields.

Update fields in nested objects If your document contains nested objects, you can use "dot notation" to reference nested fields within the document when you call update()

This would be static and result in

update({
'level1.level2.STATIC_KEY' : 'updatedValue'
});

Then I found this answer https://stackoverflow.com/a/47296152/5552695 which helped me to make the updatepath dynamic. The desired solution after this could look like

field[`level1.level2.${DYNAMIC_KEY}`] = updateValue;
update(field);

But still: it'll delete the other fields in this path.

UPDATE:

The Structure of my Doc is as follows: Snapshot

So inside this structure i want to update only complexArray > 0 > innerObject > age

Writing the above path into the update() method will delete everything else on the complexArray-level. A simple update on first-level-fields works fine and lets the other first-level-fields untouched.

Is it possible, that firestore functions like update() can only act on the lowest field-level on an document. And as soon as i put complex objects into an document its not possible to select such inner fields? I know there would be the solution to extract those "complex" objects into separate collections + documents and put these into my current lowest document level. I think this would be a more accurate way to stick to the FireStore principles. But on Application side it is easier to work with complex object than to always dig deeper in firestore collection + document structure.

So my current solution is to send the whole complex object into the update() method even though I just changed only one field on application side.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Daddelbob
  • 406
  • 4
  • 13
  • I've just tried and it works correctly. Can you share the full code you are using to update. – Renaud Tarnec Jun 03 '18 at 12:05
  • Hi @Daddelbob, Did you find a solution? – Antonio Oct 03 '18 at 18:56
  • I didn't find a solution, but I think a flatter data structure would be the first step towards desired firestore DB use. Even though it is possible to deep nest decuments and collections several times, my experience tells me this should be avoided if possible, because this will lead to more complex update operations with the limited set of firestore commands. – Daddelbob Oct 05 '18 at 08:46
  • Is complexArray a map or an array? When I test it, my other field values don't get deleted if it is a map. If it is an array, the whole array gets replace with a map. – HJo Feb 15 '19 at 06:36

1 Answers1

7

Have you tried using the { merge: true } option in your request?

db
  .collection("myCollection")
  .doc("myDoc")
  .set(
    { 
      level1: { level2: { myField: "myValue" } }
    },
    { merge: true }
  )
BearGrylls
  • 425
  • 6
  • 16