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

标签 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解决这个问题吗?

最佳答案

我猜您想一次性更新 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,让潜水员知道要更新哪个元素。

关于java - 如何使用JAVA一次更新mongo db的单个文档中的多个字段(数组字段和普通字段)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57023904/

相关文章:

java - Android-空指针异常反复崩溃

Java反射运行时性能

node.js - 将 Mongo 输出与 Node 结合起来用于 API

java - Google Cloud Bigtable 客户端连接池

java - Spring Boot 应用程序在 Cloud Foundry java.lang.IllegalStateException 上崩溃

java - html标签到ical标签格式

java - 无法在 Android 中创建 sqlite 数据库

java - Spring-Data-Mongodb 写入数据库时​​出现 NoSuchMethodError

java - 如何在Spring mvc和mongodb中下载文件

java - 使用相同参数的不同调用会产生不同的 LocalDateTime 结果