【问题标题】:update data data in EmbeddedListField as other dict field data MongoEngine将 EmbeddedListField 中的 data 数据更新为其他 dict 字段数据 MongoEngine
【发布时间】:2020-06-30 09:11:33
【问题描述】:

我正在尝试更新 doc_list 下的一个字段,该字段在 proect_list 中是一个嵌入式文档字段 使用 [ 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":""
         }
      }
   ]
}

我已经尝试过这个并在 MongoEngine 文档中尝试了很多其他示例,并且在 StackOverflow 中搜索尚未解决,请帮助我进行此更新。

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: "" } } ]})

【问题讨论】:

    标签: mongodb mongoengine restframeworkmongoengine


    【解决方案1】:

    也许这种方法对你有帮助

    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"
            }
        }
    )
    

    【讨论】:

      猜你喜欢
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多