0

My mongoDB schema as below .

"interpretationlist": [
{
    "interpretations": [
        {
            "id": "1",
            "value": "value1"
        },
        {
            "id": "2",
            "value": "value2"
        }
    ]
},
{
    "interpretations": [
        {
            "id": "3",
            "value": "value3"
        }
    ]
}
]

and I want to search interpretation whose id is 1 and update its value. How can I do this. no idea how can I do this.

Neo-coder
  • 7,715
  • 4
  • 33
  • 52
Rishabh Garg
  • 706
  • 1
  • 9
  • 28

1 Answers1

0

Hi I don't know how to do this in mongo query but I write below script which will solve your problem

         db.collectionName.find({
     "interpretationlist": {
         "$exists": true
     }
     }).forEach(function(data) {
     for (var i = 0; i < data.interpretationlist.length; i++) {
         for (var j = 0; j < data.interpretationlist[i].interpretations.length; j++) {
             var query = {};
             var updateQuery = {};
             findkey = [];
             findkey.push("interpretationlist." + i + ".interpretations." + j + ".id");
             query[findkey] = "1";
             updateQuery[findkey] = "20";
             db.collectionName.update(query, {
                 "$set": updateQuery
             }, false, false);
         }
     }
     })
Neo-coder
  • 7,715
  • 4
  • 33
  • 52