0

I'm trying to update a nested object in the firestore DB I'm using the web SDK. Here is the format of the object that I wish to update:

{
  "name": "Math",
  "sections": {
    "section1": {
      "sectionName": "Introduction",
      "lessonsObj": {
        "1": {
          "pageResource": "page resource",
          "title": "Intro"
        }
      }
    }
  },
  "description": "Lorem ipsum."
}

I'd like to update the title property for the first lesson in the lessonsObj object. The values for section1, and the lesson key number 1 will be dynamic, but I'm not sure how I can do this?

Here is the screenshot of the structure in FB Firestore:

enter image description here

Abdi
  • 490
  • 1
  • 6
  • 17
  • Are you showing the structure of a single document here? It might be easier to see what you're working with if you provide a screenshot from the console. Please edit the question to be more clear. – Doug Stevenson Jun 15 '20 at 17:19
  • If its a single document as shown in your question then following might hel[ you db.collection(CollectionName).doc("math").update({ "sections.section1.lessonsObj.1.title": "Changing title name" }) – Vipul Patil Jun 15 '20 at 17:24
  • @DougStevenson I've added a screenshot from the console. – Abdi Jun 15 '20 at 17:35

1 Answers1

2

You can use dot notation to build a path to a field that is nested in maps, as described in the documentation.

document.update({
    "JavaScript.sections.section1.1.title": "value"
})

You will need to provide the full path as a string. You can build that string any way you want, using variables as necessary. You will just need to be able to call out the full name of the value.

This will not work if any of the nested objects is an array.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441