MongoDB - 更新文档列表中的对象

时间:2016-10-22 07:27:43

标签: java mongodb database

我有以下文件清单List<Project> projectList

"projectsList": [
    {
        "projectNumber": 1
        "projectName": "kjsd",
        "projectURL": "lkjsand",
        "projectDate": "Oct 7, 2016 12:00:00 AM",
        "projectOccupation": "kaijiuhdsa",
        "projectProposal": "aus asdig agduya"
    },
    {
        "projectNumber": 2
        "projectName": "alan walker",
        "projectURL": "lkjsand",
        "projectDate": "Oct 7, 2016 12:00:00 AM",
        "projectOccupation": "kaijiuhdsa",
        "projectProposal": "aus asdig agduya"
    },
    {
        "projectNumber": 3
        "projectName": "",
        "projectURL": "",
        "projectOccupation": "",
        "projectProposal": ""
    }

]

我有两个包含

的项目对象
Project oldProject= new Project("kjsd","lkjsand","Oct 7, 2016 12:00:00 AM","kaijiuhdsa","aus asdig agduya")

我需要更新

Project newProject= new Project("kjsd","http","Oct 7, 2016 12:00:00 AM","kaijiuhdsa","ocr")

如何使用java驱动程序在mongoDB中使用newProject更新oldProject?

1 个答案:

答案 0 :(得分:1)

我可以在MongoDB中回答。我想你可以把它转换成适当的Java方法。

db.col.update(
    {"projectsList.projectNumber": 1}, 
    {$set: {
      "projectsList.$.projectUrl": "http",
      "projectsList.$.projectProposal": "ocr"
    }})