How to update nested array in complex schema

how to push new object in G1>P1C1> relies array…

db.test1.insertMany([
      {
        _id: 'G1',
        name: 'My Group',
        posts: [
          {
            _id: 'P1',
            title: 'Post 1',
            comments: [
              {
                _id: 'C1',
                name: 'Comment 1',
                replies: [
                  {
                    _id: 'R1',
                    content: 'Reply 1',
                  },
                  {
                    _id: 'R2',
                    content: 'Reply 2',
                  },
                ],
              },
              {
                _id: 'C2',
                name: 'Comment 2',
                replies: [
                  {
                    _id: 'R3',
                    content: 'Reply 3',
                  },
                  {
                    _id: 'R4',
                    content: 'Reply 4',
                  },
                ],
              },
            ],
          },
        ],
      },
      {
        _id: 'G2',
        name: 'My Group',
        posts: [
          {
            _id: 'P2',
            title: 'Post 2',
            comments: [
              {
                _id: 'C3',
                name: 'Comment 3',
                replies: [
                  {
                    _id: 'R5',
                    content: 'Reply 5',
                  },
                  {
                    _id: 'R6',
                    content: 'Reply 6',
                  },
                ],
              },
              {
                _id: 'C4',
                name: 'Comment 4',
                replies: [
                  {
                    _id: 'R7',
                    content: 'Reply 7',
                  },
                  {
                    _id: 'R8',
                    content: 'Reply 8',
                  },
                ],
              },
            ],
          },
        ],
      },
    ]);

Hello : )

The json bellow was not valid,because sometimes its hard to do it by hand you can
check using a tool to be valid json.
I randomly used https://jsonformatter.org/json-pretty-print to fix that document.
(didn’t had “” on keys or values,had extra commas etc)

I pushed(add at the end) a new document inside G1->P1->C1->Relies
You can see that document i added inside the pipeline of the update command.

{"_id" : "R3" ,
 "content" : "Reply 3",
 "randomField" : "EDIT THIS DOC"}

Update command(you can only take the pipeline the “u” part and use any driver update command),
as long its mongoDB>=4.2 compatable,becaused i used pipeline in the update.


{
  "update": "testcoll",
  "updates": [
    {
      "q": {},
      "u": [
        {
          "$replaceRoot": {
            "newRoot": {
              "$cond": [
                {
                  "$eq": [
                    "$$ROOT._id",
                    "G1"
                  ]
                },
                {
                  "$mergeObjects": [
                    "$$ROOT",
                    {
                      "posts": {
                        "$let": {
                          "vars": {
                            "posts": "$$ROOT.posts"
                          },
                          "in": {
                            "$map": {
                              "input": "$$posts",
                              "as": "post",
                              "in": {
                                "$cond": [
                                  {
                                    "$eq": [
                                      "$$post._id",
                                      "P1"
                                    ]
                                  },
                                  {
                                    "$mergeObjects": [
                                      "$$post",
                                      {
                                        "comments": {
                                          "$map": {
                                            "input": "$$post.comments",
                                            "as": "comment",
                                            "in": {
                                              "$cond": [
                                                {
                                                  "$eq": [
                                                    "$$comment._id",
                                                    "C1"
                                                  ]
                                                },
                                                {
                                                  "$mergeObjects": [
                                                    "$$comment",
                                                    {
                                                      "replies": {
                                                        "$concatArrays": [
                                                          "$$comment.replies",
                                                          [
                                                            {
                                                              "_id": "R3",
                                                              "content": "Reply 3",
                                                              "randomField": "EDIT THIS DOC"
                                                            }
                                                          ]
                                                        ]
                                                      }
                                                    }
                                                  ]
                                                },
                                                "$$comment"
                                              ]
                                            }
                                          }
                                        }
                                      }
                                    ]
                                  },
                                  "$$post"
                                ]
                              }
                            }
                          }
                        }
                      }
                    }
                  ]
                },
                "$$ROOT"
              ]
            }
          }
        }
      ],
      "multi": true
    }
  ]
}


After the update my cursor returned those 2 documents

[
  {
    "_id": "G1",
    "name": "My Group",
    "posts": [
      {
        "_id": "P1",
        "title": "Post 1",
        "comments": [
          {
            "_id": "C1",
            "name": "Comment 1",
            "replies": [
              {
                "_id": "R1",
                "content": "Reply 1"
              },
              {
                "_id": "R2",
                "content": "Reply 2"
              },
              {
                "_id": "R3",
                "content": "Reply 3",
                "randomField": "EDIT THIS DOC"
              }
            ]
          },
          {
            "_id": "C2",
            "name": "Comment 2",
            "replies": [
              {
                "_id": "R3",
                "content": "Reply 3"
              },
              {
                "_id": "R4",
                "content": "Reply 4"
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "_id": "G2",
    "name": "My Group",
    "posts": [
      {
        "_id": "P2",
        "title": "Post 2",
        "comments": [
          {
            "_id": "C3",
            "name": "Comment 3",
            "replies": [
              {
                "_id": "R5",
                "content": "Reply 5"
              },
              {
                "_id": "R6",
                "content": "Reply 6"
              }
            ]
          },
          {
            "_id": "C4",
            "name": "Comment 4",
            "replies": [
              {
                "_id": "R7",
                "content": "Reply 7"
              },
              {
                "_id": "R8",
                "content": "Reply 8"
              }
            ]
          }
        ]
      }
    ]
  }
]

Hope it helps.

1 Like

It is very helpful for me thank so much for help …