0

I'm having trouble updating a nested array in a Firebase database. I was able to do it on an array using arrayUnion, but not sure how or if possible on a nested array.

My data structure is as follows..

parent_id (document)

--name (string)

--children (array)

----0 (object)

------child_id (string)

------child_name (string)

------classes (array)

--------0 (object)

----------class_id (string)

----------class_name (string)

--------1 (object)

----------class_id (string)

----------class_name (string)

----1 (object)

------child_id (string)

------child_name (string)

------classes (array)

--------0 (object)

----------class_id (string)

----------class_name (string)

--------1 (object)

----------class_id (string)

----------class_name (string)

etc

To add a new child to the children array I use the following..

 firebase
  .firestore()
  .collection("users")
  .doc(parent_id)
  .update({
    children: firebase.firestore.FieldValue.arrayUnion({
      child_id: child_id,
      child_name: child_name
    })
  });

I cannot work out how to add a class to the classes array which is nested inside a child

Hopefully it is possible, I'm starting to think I should have used sub-collections instead of nested arrays!

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
JakeKempo
  • 580
  • 1
  • 5
  • 14

0 Answers0