【问题标题】:How to update multiple fields(array field and normal field) in a single document of mongo db at a time using JAVA?如何使用JAVA一次更新mongo db的单个文档中的多个字段(数组字段和普通字段)?
【发布时间】:2019-07-14 01:17:28
【问题描述】:

我只想使用 java 更新数组中的请求字段。这是我在 mongo db 中的现有文档:

{
    "_id": "6691e5068dwe335w42cb0a699650f",
    "Opportunity_Owner": "Self",
    "Account_Name": "IC",
    "Lead_Source": "Callbox",
    "Opportunity_Name": "name1 ",
    "Stage": "Proposal",
    "Stage_Status": "A",
    "1555570551211": [],
    "1555556165153": [],
    "1555556059584": [{
            "id": "1557389940585",
            "Notes": "Note 1"
        },
        {
            "id": "1557389945398",
            "Notes": "Hi Bobby "
        },
        {
            "id": "1557389978181",
            "Notes": "Spoken to Bobby."
        },
        {
            "id": "1557389990159",
            "Notes": "plan to call on 29/Apr"
        }
    ],

    "createdBy": "2c18b8dbb7d74a41a66f53a90117480a",
    "createdDate": "1562911250917"
}

请求负载:

{
 "_id" : "6691e5068dwe335w42cb0a699650f",
 "Stage_Status" : "I",
 "1555556059584" : [ 
        {
            "id" : "1557389940585",
            "Notes" : "updated note 123"
        }
     ]
}

我正在尝试使用 $set 一次更新“Stage_Status”和“1555556059584.Notes”。我可以更新“Stage_Status”,但“1555556059584”数组将使用我上次更新的内容进行重置。

预期输出:

 {
        "_id" : "6691e5068dwe335w42cb0a699650f",
        "Opportunity_Owner" : "Self",
        "Account_Name" : "IC",
        "Lead_Source" : "Callbox",
        "Opportunity_Name" : "name1 ",
        "Stage" : "Proposal",
        "Stage_Status" : "I",
        "1555570551211" : [],
        "1555556165153" : [],
        "1555556059584" : [ 
            {
                "id" : "1557389940585",
                "Notes" : "updated note 123"
            }, 
            {
                "id" : "1557389945398",
               "Notes" : "Hi Bobby "
            }, 
            {
                "id" : "1557389978181",
                "Notes" : "Spoken to Bobby."
            }, 
            {
                "id" : "1557389990159",
                "Notes" : "plan to call on 29/Apr"
            }
        ],

        "createdBy" : "2c18b8dbb7d74a41a66f53a90117480a",
        "createdDate" : "1562911250917"
    }

谁能帮我在java中弄明白。

【问题讨论】:

    标签: java mongodb spring-boot mongodb-query mongodb-java


    【解决方案1】:

    我猜你想立即更新 Stage_Status1555556059584.Notes
    这是一个关于它的演示

    > db.student.find()
    { "_id" : ObjectId("5d2c09ea8ed60ae70d3dd76b"), "name" : "bigbang", "courses" : [ { "name" : "en", "classRoom" : "9001" }, { "name" : "math", "classRoom" : "1001" } ] }
    > db.student.update({name:'bigbang','courses.name':'en'},{ $set: {'courses.$.classRoom':'1009',name :"course"} })
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.student.find()
    { "_id" : ObjectId("5d2c09ea8ed60ae70d3dd76b"), "name" : "course", "courses" : [ { "name" : "en", "classRoom" : "1009" }, { "name" : "math", "classRoom" : "1001" } ] }
    

    java demo是这样的

    collection.updateOne(and(eq("Stage_Status","A"),eq("1555556059584.id","1557389940585")),new Document("$set" ,new Document("Stage_Status","YOUR_NEW_VALUE").append("1555556059584.$.Notes","YOUR_NEW_VALUE")));
    
    

    您必须设置1555556059584.id 让潜水员知道要更新哪个元素。

    【讨论】:

      猜你喜欢
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      • 2011-02-06
      相关资源
      最近更新 更多