0

How to updating/adding sub-document in sub-document in mongoose, I have sample data for Tree Json, like this:

orgchart:
 [
  {
    level: 0,
    name: Division 0,
    child: [
     {
       level: 1,
       nama: Division 1,
       child: [
        {
          level: 2,
          nama: Sub Division 2,
          child: [ here division X ]
        }
       ]
     }
  },
 ]

I tried use parent.push but only add 1 sub document, when i tried to use more sub document in sub document always failed with message "TypeError: Cannot call method 'push' of undefined"

Do you have clue for this problem, Thanks

Rampak
  • 115
  • 1
  • 11

2 Answers2

0

In this case you're probably best off grabbing calling .findOne() to get the document contain the sub-document that you want to update, pushing the subdocument to the array, then calling .save() on the document.

Really, if you have sub-documents with embedded sub-documents, you should consider creating a collection for the sub documents, and possibly the documents embedded in those sub-documents. You can store _id references in the parent document and invoke populate(...) with your query.

I would provide an example, though I'm not certain what you're trying to accomplish. Initially it looks like bad data modeling, but I'm not sure exactly what you're trying to accomplish.

tsturzl
  • 3,089
  • 2
  • 22
  • 35
0

Sorry to bother you mr tsturzl I got solution from Mongodb update deeply nested subdocument

Thank You

Community
  • 1
  • 1
Rampak
  • 115
  • 1
  • 11