1

Below is the same MongoDB data:

{  
   "_id":{  
      "$oid":"5c228169e8cab71a7132c82b"
   },
   "param1":"ABC",
   "param2":[  
      {  
         "aid":{  
            "$oid":"5c228169e8cab7139d10aca7"
         },
         "param3":"DEF",
         "param4":[  
            {  
               "lid":{  
                  "$oid":"5c2281685ee3358440f9412f"
               },
               "param5":"GHI"
            },
            {  
               "lid":{  
                  "$oid":"5c22816d5ee3358440f941bb"
               },
               "param5":"JKL"
            },
         ]
      },
      {  
         "aid":{  
            "$oid":"5c22819de8cab71a7d6db75d"
         },
         "param3":"MNO",
         "param4":[ 
            {  
               "lid":{  
                  "$oid":"5c2281735ee3358440f9428a"
               },
               "param5":"QRS"
            },
            {  
               "lid":{  
                  "$oid":"5c2281795ee3358440f9431a"
               },
               "param5":"TUV"
            }
         ]
      }
   ]
}

Problem: How to change values or add any new key-value pair in the param4 array list, like if "param5":"JKL" need to be changed to "param5":"XYZ" or adding a new key-value pair "param6":"Hello" alongwith "param5":"JKL" so that member array become like:

{  
   "lid":{  
      "$oid":"5c22816d5ee3358440f941bb"
    },
   "param5":"JKL"
   "param6":"Hello"
}

I have checked different solutions but all work up to the stage of param4 but not inside the it. Please share any solution

Akhilesh
  • 1,064
  • 15
  • 28

2 Answers2

1

You can take advantage of $[<identifier>] (positional filtered operator) syntax with arrayFilters

To update param5:

db.col.update(
    { _id: { "$oid":"5c228169e8cab71a7132c82b" } }, 
    { $set: { "param2.$[cond1].param4.$[cond2].param5": "XYZ" } }, 
    { arrayFilters: [ { "cond1.aid.$oid": "5c228169e8cab7139d10aca7" }, { "cond2.lid.$oid": "5c22816d5ee3358440f941bb" } ]  })

Similarly you can use $set to add new key - param6

mickl
  • 48,568
  • 9
  • 60
  • 89
  • Solutions worked in Mongo shell, with php mongodb client equivalent query code throwing error: Uncaught MongoDB\Driver\Exception\InvalidArgumentException: "arrayFilters" option has invalid keys for a BSON array. I am trying to figure it out. – Akhilesh Dec 26 '18 at 20:58
  • 1
    got it working in php mongodb client query code too. – Akhilesh Dec 26 '18 at 21:29
0

This will work for me You can try this

db.collection.find({"_id.oid": "5c228169e8cab71a7132c82b"}).forEach(function(ele){
   for(var i = 0; i < ele.param2.length; i++){
        var prm4=ele.param2[i].param4;             
              for(var j = 0; j < prm4.length; j++){
                   if(prm4[j].param5 == "JKL") 
                    {
                                var data = {};
                                data["param2." + i.toString() + ".param4."+j.toString()+ ".param5"]="XYZ"
                                data["param2." + i.toString()+ ".param4."+j.toString()+ ".param6"]="Hello"

                                db.collection.update({ "_id.oid": "5c228169e8cab71a7132c82b"},{$set:data})                                   

                     } 
} } });
Anushka Ahir
  • 136
  • 7