Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to update multiple array elements in mongodb

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.78k
    Comment on it

    Hello Everyone,


    In this blog we will discuss about how to update multiple array elements in MongoDb using a single query.

    Let say we have a collection in MongoDb named "Form" which consists of the below available documents:

    > db.Form.find().pretty()
    {
        "_id" : ObjectId("5637345bb8bd53700e2ec054"),
        "title" : "Customer Complaints",
        "formData" : [
            {
                "minimise" : true,
                "label" : "test",
                "value" : "",
                "type" : "textInput",
                "required" : false,
                "description" : "Eg.Josh",
                "metaData" : {
                    "placeholder" : "Enter name"
                }
            },
            {
                "label" : "test",
                "value" : "",
                "type" : "textArea",
                "required" : false,
                "description" : "eg.street one",
                "metaData" : {
                    "placeholder" : "placeholder"
                }
            },
            {
                "label" : "test",
                "value" : "",
                "type" : "select",
                "required" : false,
                "description" : "description",
                "metaData" : {
                    "placeholder" : "placeholder",
                    "options" : [
                        "Male",
                        "Female"
                    ]
                }
            }
        ],
        "userId" : "sf12h12a412s",
        "userName" : "Ankit Chettri"
    }
    {
        "_id" : ObjectId("56373832b8bd53700e2ec057"),
        "title" : "User requirements",
        "formData" : [
            {
                "label" : "test",
                "value" : null,
                "type" : "sliderInput",
                "required" : false,
                "description" : "Range",
                "metaData" : {
                    "ceiling" : 10000,
                    "floor" : 1000,
                    "step" : "1000"
                }
            },
            {
                "label" : "test",
                "value" : "",
                "type" : "switchInput",
                "required" : false,
                "description" : "options",
                "metaData" : {
                    "switch_value_one" : "Yes",
                    "switch_value_two" : "No"
                }
            }
        ],
        "userId" : "sf12h12a412s",
        "userName" : "Ankit Chettri"
    }
    

    If you want to update the content of label of multiple array elements in the above (Form)documents in one query below is the query:-

     db.Form.find().forEach(function (Form) {Form.formData.forEach(function (formData) {formData.label="Name";db.Form.save(Form);});})
    

    Output

    > db.Form.find().pretty()
    {
        "_id" : ObjectId("5637345bb8bd53700e2ec054"),
        "title" : "Customer Complaints",
        "formData" : [
            {
                "minimise" : true,
                "label" : "Name",
                "value" : "",
                "type" : "textInput",
                "required" : false,
                "description" : "Eg.Josh",
                "metaData" : {
                    "placeholder" : "Enter name"
                }
            },
            {
                "label" : "Name",
                "value" : "",
                "type" : "textArea",
                "required" : false,
                "description" : "eg.street one",
                "metaData" : {
                    "placeholder" : "placeholder"
                }
            },
            {
                "label" : "Name",
                "value" : "",
                "type" : "select",
                "required" : false,
                "description" : "description",
                "metaData" : {
                    "placeholder" : "placeholder",
                    "options" : [
                        "Male",
                        "Female"
                    ]
                }
            }
        ],
        "userId" : "sf12h12a412s",
        "userName" : "Ankit Chettri"
    }
    {
        "_id" : ObjectId("56373832b8bd53700e2ec057"),
        "title" : "User requirements",
        "formData" : [
            {
                "label" : "Name",
                "value" : null,
                "type" : "sliderInput",
                "required" : false,
                "description" : "Range",
                "metaData" : {
                    "ceiling" : 10000,
                    "floor" : 1000,
                    "step" : "1000"
                }
            },
            {
                "label" : "Name",
                "value" : "",
                "type" : "switchInput",
                "required" : false,
                "description" : "options",
                "metaData" : {
                    "switch_value_one" : "Yes",
                    "switch_value_two" : "No"
                }
            }
        ],
        "userId" : "sf12h12a412s",
        "userName" : "Ankit Chettri"
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: