update data data in EmbeddedListField as other dict field data MongoEngine

64 Views Asked by At

I am trying to update a field under doc_list which in proect_list is an embeddeddocumentfield using [ mongoengine ]

   "_id":{
      "$oid":"5efae3a302449b643b2e5a06"
   },
   "username":"Roshan",
   "project_list":[
      {
         "project_id":"216198",
         "customername":"Gopal",
         "customer_region":"IND",
         "doc_list":{
            "status":"Inprogress",
            "date_modified":{
               "$date":"2020-06-30T12:32:59.851Z"
            },
            "date_started":{
               "$date":"2020-06-30T12:32:59.851Z"
            },
            "nrfu_doc_id":"",
            "reference_doc_id":""
         }
      },
      {
         "project_id":"624615",
         "customername":"xcisco",
         "customer_region":"IND",
         "doc_list":{
            "status":"Completed",
            "date_modified":{
               "$date":"2020-06-30T12:35:06.031Z"
            },
            "date_started":{
               "$date":"2020-06-30T12:35:06.031Z"
            },
            "nrfu_doc_id":"",
            "reference_doc_id":""
         }
      }
   ]
}

I have tried this and tried lot of other examples in MongoEngine Documentation and also Searched in StackOverflow not yet resolved, Please Help me to do this Update.

users = PersonProjectCollection.objects(username="Roshan", project_list__project_id="216198").update_one(set__project_list__doc_list__S__reference_doc_id='21333')
mongoengine.errors.OperationError: Update failed (Cannot create field 'doc_list' in element {project_list: [ { project_id: "216198", customername: "Gopal",
 customer_region: "IND", doc_list: { status: "Inprogress", date_modified: new Date(1593520379851), date_started: new Date(1593520379851), nrfu_doc_id: "",
reference_doc_id: "" } }, { project_id: "624615", customername: "xcisco", customer_region: "IND", doc_list: { status: "Completed", date_modified: new Date(
1593520506031), date_started: new Date(1593520506031), nrfu_doc_id: "", reference_doc_id: "" } } ]})
1

There are 1 best solutions below

0
Maryam On

maybe this approach help you

index = 0
users = PersonProjectCollection._get_collection.update_one(
    {
        "username":"Roshan",
        "project_list.project_id":"216198"
    },
    {
        "$set": {
            "project_list.%d.doc_list.reference_doc_id" % index: "21333"
        }
    }
)