给出一些背景- 我正在制作一个错误跟踪器应用程序,并使用MongoDB进行数据存储。我需要更新文档中的单个值-错误的状态。我的“ bug”对象存储在mongoDB中。目前,我正在通过其ID值检索文档/对象/错误。然后,我将通过JVM更新对象,并重新保存/覆盖整个原始文档/错误,以便它现在包含更新的状态。虽然这样做有效,但我不确定这是否是在MongoDB中更新数据的“专业”方式。预先感谢您的建议/意见。
@PutMapping("/{bugID}/{status}")
public Bug setBugStatus(@PathVariable String projectID, @PathVariable String bugID, @PathVariable String status) {
return bugService.setBugStatus(projectID, bugID, status);
}
public Bug setBugStatus(String projectID, String bugID, String status) {
Project project = getProjectByID(projectID);
Bug bug = project.getBugQueue().findBugByID(bugID);
if (status.equalsIgnoreCase("solved")) {
bug.setStatusSolved();
} else if (status.equalsIgnoreCase("inprogress")) {
bug.setStatusInProgress();
} else if (status.equalsIgnoreCase("unsolved")) {
bug.setStatusUnsolved();
}
mongoTemplate.save(project);
return bug;
}
private Project getProjectByID(String projectID) {
return mongoTemplate.findById(projectID, Project.class);
}
这是文档的样子:
{
"projectID": "1",
"title": "Amazon",
"description": "An online ECommerce store.",
"dateCreated": "Mon Sep 14 01:18:40 IST 2020",
"timestamp": "14-09-2020 01:18:40",
"bugQueue": {
"priorityQueue": [
{
"priority": 10,
"bug": {
"bugID": "1",
"dateCreated": "Mon Sep 14 01:18:40 IST 2020",
"dateResolved": null,
"title": "Order processing failed.",
"bugDescription": "Orders are not being processed after 9.32pm.",
"status": "Unsolved",
"discoveredBy": null,
"solutionDescription": null,
"bugProgressReportList": {
"reportsList": []
},
"bugAssignedToList": {
"employeeList": []
},
"priority": "Highest"
}
}
],
"empty": false
},
"employeeList": {
"employeeList": [
{
"employeeID": "2",
"firstName": "Alex",
"secondName": "Wands"
},
{
"employeeID": "3",
"firstName": "Alice",
"secondName": "Black"
}
]
}
}