Update nested array value in mongodb java -


this database structure

 { "_id" : objectid("58243c430386650d78d12d53"), "email" : "technotest91@gmail.com", "dentistname" : "suzuka", "tempdetails" : [      {         "tempname" : "elsa",         "city" : "bangalore",          "tempemail": "abc@gmail.com",         "experience" : "5",             "hireddates" : [              {                 "status" : "accepted",                 "bookingid" : "36y0ym",             }         ],     },      {         "tempname" : "elsa",           "city" : "bangalore",          "tempemail": "abc@gmail.com",         "experience" : "5",            "hireddates" : [              {                  "status" : "hired",                 "bookingid" : "92qhd7",             }         ],       },      {         "tempname" : "elsa",           "city" : "bangalore",         "experience" : "5",         "tempemail": "abc@gmail.com",         "typeofpractice" : "oral surgery",         "hireddates" : [              {                 "status" : "hired",                 "bookingid" : "95kte9",             }         ],     } ]  } 

i trying update status based on bookingid. not able update value . have tried following code in java mongodb not seem updating.

    basicdbobject searchquery = new basicdbobject();      list<basicdbobject> andquery = new arraylist<basicdbobject>();     andquery.add(new basicdbobject("email", dentalemail));     andquery.add(new basicdbobject("tempdetails.tempemail",tempemail));       searchquery.put("$and", andquery);      dbcursor cursor = col.find(searchquery);      if (cursor.count() != 0) {     while (cursor.hasnext()) {          basicdblist jobstatuslist = (basicdblist) cursor.next().get("tempdetails");              for(int k=0;k<jobstatuslist.size();k++)             {                 basicdbobject tempjobobject = (basicdbobject) jobstatuslist.get(k);                  basicdblist hireddateslist = (basicdblist) tempjobobject.get("hireddates");                   for(int =0; i<hireddateslist.size();i++)                 {                     basicdbobject hireddatesobject = (basicdbobject) hireddateslist.get(i);                     string dbbookingid = hireddatesobject.getstring("bookingid");                      if(dbbookingid.equalsignorecase(bookingid))                     {                         basicdbobject doc = new basicdbobject("$set",                                 new basicdbobject().append("tempdetails."+k+".hireddates."+i+".status", status));                          col.update(searchquery, doc, false, false);                          system.out.println(doc);                      }else{                         result = false;                     }                 } 

kindly me out issue have been struggling update record based on bookingid. document through query , iterate on objects , if bookingid matches updating record. kindly let me know should changing..

did checked database ? able run code fine without errors , update collection well.

i created method doing following :

private void performoperation() {     mongoclient mongo = new mongoclient("localhost", 27017);     mongoclient mongoclient = new mongoclient();     db db = mongoclient.getdb("test");     dbcollection table = db.getcollection("vinay");      logger.info("connected successfully");      basicdbobject searchquery = new basicdbobject();      list<basicdbobject> andquery = new arraylist<basicdbobject>();     andquery.add(new basicdbobject("email", "technotest91@gmail.com"));     andquery.add(new basicdbobject("tempdetails.tempemail", "abc@gmail.com"));      searchquery.put("$and", andquery);      logger.info("executing :"+ searchquery);      dbcursor cursor = table.find(searchquery);     if (cursor.count() != 0) {         while (cursor.hasnext()) {              basicdblist jobstatuslist = (basicdblist) cursor.next().get("tempdetails");              (int k = 0; k < jobstatuslist.size(); k++) {                 basicdbobject tempjobobject = (basicdbobject) jobstatuslist.get(k);                  basicdblist hireddateslist = (basicdblist) tempjobobject.get("hireddates");                  (int = 0; < hireddateslist.size(); i++) {                     basicdbobject hireddatesobject = (basicdbobject) hireddateslist.get(i);                     string dbbookingid = hireddatesobject.getstring("bookingid");                      if (dbbookingid.equalsignorecase("36y0ym")) {                         basicdbobject doc = new basicdbobject("$set", new basicdbobject()                                 .append("tempdetails." + k + ".hireddates." + + ".status", "new status"));                          table.update(searchquery, doc, false, false);                          logger.info(doc);                      } else {                         logger.info("no record find");                     }                 }             }         }     } } 

and after execute status booking id 36y0ym updated - "new status".

but noticed 1 this. way wrote loops... because after record updated iterates again , prints message "no record found" in case set result false, if relying on result whole approach wrong.

i dont know logic trying build point there nothing wrong in code updates records in mongodb if there wrong loops wrote.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -