如何使用JAVA一次更新mongo db的单个文档中的多个字段(数组字段和普通字段)?

时间:2019-07-14 01:17:28

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

我只想使用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弄清楚吗。

1 个答案:

答案 0 :(得分:0)

我想您想一次更新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演示就是这样

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以便让潜水员知道要更新哪个元素。