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

3
  • 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. Commented Jun 15, 2020 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" }) Commented Jun 15, 2020 at 17:24
  • @DougStevenson I've added a screenshot from the console. Commented Jun 15, 2020 at 17:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I missed the dot notation part, thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.