Update an object in nested array in MongoDB

Hello Reader ?

To update an object inside a nested array in MongoDB you can use this approach, Its well tested and working.

Case:

Update nested array objects. See below picture for a better Idea what we are going to do. We will update the …


This content originally appeared on DEV Community and was authored by Rajesh Royal

Hello Reader ?

To update an object inside a nested array in MongoDB you can use this approach, Its well tested and working.

Case:

Update nested array objects. See below picture for a better Idea what we are going to do. We will update the object which is nested by 3 levels of array.

{
 discussionList[
  discussionList [
   {
     object-value-to-be-updated.
   }
  ]
 ]
} 

mongodb - update an object inside multilevel nested array

Solution:

public courseCategoryPostCommentReplyUpdate(operation: CommentReplyUpdateMutation): Promise<IDocumentUpdateType> {
    return this.courseCategoryPostCommentsModel.updateOne(
      {
        "postId" : operation.postId,
        'discussionList': {
          '$elemMatch': {
            '_id': operation.commentId,
            "discussionList._id": operation.replyId
          }
        }
      },
      {
        $set: {
          "discussionList.$[outer].discussionList.$[inner].payload": operation.payload,
          "discussionList.$[outer].discussionList.$[inner].isUpdated": true,
          "discussionList.$[outer].discussionList.$[inner].commentUpdateTime": new Date()
        }
      },
      {
        arrayFilters: [
          { "outer._id": operation.commentId},
          {"inner._id": operation.replyId}

      ]
      }
    );
  }

FootNotes - this code is to update the reply of a comment.

That's how you can perform operations on an object in nested array in mongoDB document. You can also update/delete the objects which are having the more level of nesting by just modifying the query.

Please let me know if the code needs an explanation. Thanks ?


This content originally appeared on DEV Community and was authored by Rajesh Royal


Print Share Comment Cite Upload Translate Updates
APA

Rajesh Royal | Sciencx (2021-09-28T14:06:46+00:00) Update an object in nested array in MongoDB. Retrieved from https://www.scien.cx/2021/09/28/update-an-object-in-nested-array-in-mongodb/

MLA
" » Update an object in nested array in MongoDB." Rajesh Royal | Sciencx - Tuesday September 28, 2021, https://www.scien.cx/2021/09/28/update-an-object-in-nested-array-in-mongodb/
HARVARD
Rajesh Royal | Sciencx Tuesday September 28, 2021 » Update an object in nested array in MongoDB., viewed ,<https://www.scien.cx/2021/09/28/update-an-object-in-nested-array-in-mongodb/>
VANCOUVER
Rajesh Royal | Sciencx - » Update an object in nested array in MongoDB. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/28/update-an-object-in-nested-array-in-mongodb/
CHICAGO
" » Update an object in nested array in MongoDB." Rajesh Royal | Sciencx - Accessed . https://www.scien.cx/2021/09/28/update-an-object-in-nested-array-in-mongodb/
IEEE
" » Update an object in nested array in MongoDB." Rajesh Royal | Sciencx [Online]. Available: https://www.scien.cx/2021/09/28/update-an-object-in-nested-array-in-mongodb/. [Accessed: ]
rf:citation
» Update an object in nested array in MongoDB | Rajesh Royal | Sciencx | https://www.scien.cx/2021/09/28/update-an-object-in-nested-array-in-mongodb/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.