Update nested array

const GroupPost = new Schema({
  groupName: {
    type: String,
    required: true,
  },
  groupPost: [
    {
      id: {
        type: String,
        required: true,
      },
      userName: {
        type: String,
        required: true,
      },
      userPhoto: {
        type: File,
      },
      commentSection : [
        {
            comment : {
                type : String
            },
            reply : [
                {content : String}
            ]
        }
      ] 
    },
  ],
})

How to update reply element

Anybody help me

Welcome to the community, @Praveen_Gupta! :wave:

Let’s start solving this by making an example dataset:

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',
              },
            ],
          },
        ],
      },
    ],
  },
]);

Assume, we want to update G2->C4->R7 document, by adding a flag { modified=true } to it.
To achieve this, we can use arrayFilters, and the final update operation will look like this:

db.test1.updateOne(
  {
    _id: 'G2',
  },
  {
    $set: {
      'posts.$[post].comments.$[comment].replies.$[reply].modified': true,
    },
  },
  {
    arrayFilters: [
      {
        'post._id': {
          $eq: 'P2',
        },
      },
      {
        'comment._id': {
          $eq: 'C4',
        },
      },
      {
        'reply._id': {
          $eq: 'R7',
        },
      },
    ],
  },
);

Problem solved!
Now, the target reply object (R7) looks like this:

{
  '_id': 'R7',
  'content': 'Reply 7',
  'modified': true
},
1 Like

I want push a new reply in G1>P1>C1