go – How to use array filters of mongodb 3.6 in mgo framework?-ThrowExceptions

Exception or error:

Hi i have a situation where i have to update objects in nested array in mongodb collection using mgo framework of Go. As of now mongodb version 3.6 support this using ArrayFilters as a 3rd parameter in Collection.Update().The mgo Collection.Update() function only supports 2 parameters, one to find and another for update. How can i use arrayfilters in mgo’s Collection.Update()

This is my object look like

{
  "TPID": "",
  "TPName":"",
  "TCID":"",
  "TCName":"",
  "BatchId":"",
  "BatchName":"",
  "QP":[{
    "qpCode":"",
       "jobRole":"",
       "Sector":"",
       "SectorID":"",

    "CompulsoryNos":[{
    "NosCode":"",
    "TotalMarks":"",
           "OutOf":"",
           "Theory":"",
           "SkillsPracticals":"",

            "PerformanceCriteria":[{
            "CriteriaDesc":"",
            "OutOf":"",
            "Theory":"",
            "Skills":""
            }]
    }]

  }]


}

and this was my approach in mongo shell(version 3.6)

db.test4.update({},
{"$set" : 
{"QP.$[i].CompulsoryNos.$[j].PerformanceCriteria.$[k].Skills" : 
"Talented"}},
{"arrayFilters" : [{"i.qpCode" : "ID_1"},{"j.NosCode" : "Code_1"}, 
{"k.Skills" : "skills_1"}]})

is there is any way to implement the same query using mgo?

How to solve:

https://github.com/laoluan/mgo
I modified the mgo version

_ = session.DB("youdbname").C("test4").UpdateUseAf(
    bson.M{},
    bson.M{"$set": bson.M{"QP.$[i].CompulsoryNos.$[j].PerformanceCriteria.$[k].Skills": "Talented"}},
    []bson.M{
        {"i.qpCode" : "ID_1"},
        {"j.NosCode" : "Code_1"},
        {"k.Skills" : "skills_1"},
    },
)

Leave a Reply

Your email address will not be published. Required fields are marked *