How to update the existing fields, embedded mongodb documents in an array in Java

48 Views Asked by At

I have document structure

{
    "Name" : "MongoDB",
    "Client" : "Database",
    "Details" : [ 
        {
            "Date" : "2018-07-18",
            "Code" : {
                "new" : "5",
                "new 1" : "3",
                "new 2" : "4"
            }
        }, 
        {
            "Date" : "2018-07-19",
            "Code" : {
                "new" : "7",
                "new 2" : "4"
            }
        }
  ]
}

I want to update the field "new":"5" as "Devops":"2, Based on "Name":"Mongodb","Date":"2018-07-18".

1

There are 1 best solutions below

1
On

I tried this in Java,but this code appends the new document for the same date

Bson filter =new Document("Name",name);

    Document h=new Document();
    Document dd=new Document();
    for(int i=0;i<ccode.length;i++)
      {
        h.append(ccode[i], hour[i]);
      }
     dd.append("Date", d);
     dd.append("Code", h);
    Document doc=new Document().append("Details",dd);
    System.out.println(dd);
    Bson updateOperationDocument = new Document("$push", doc);
    collection.updateOne(filter, updateOperationDocument);