Data Updation in MongoDB

While updating data in document, we will come across various scenarios, let us analyze them step by step.

  1. Now, we will update field name with some other value like ABC, below find the code snippet for the same.

    Data Updation in MongoDB


    The updated output is:

    Data Updation in MongoDB

    The value of field name is updated from xyz to ABC.

  2. Considering a scenario, where we have an array for field friends

    Data Updation in MongoDB


    and if we want to update that array by adding values, we can do it by referring the below code snippet:

    Data Updation in MongoDB


    and Amit is added to the array against field friends.

    Data Updation in MongoDB

  3. We can even add object to the array of object using update query. For an example, for against field comments, if we want to add multiple objects with various field, it is possible.

    Here is the code snippet:

    Data Updation in MongoDB


    and the corresponding output is:

    Data Updation in MongoDB