我有这个mongodb模型:
div
我从简单的文档插入开始:
db.businesslinedashboards.find()
let businessLineDashboardRow = new Schema({
ref: {
type: String
},
riskstatements: {
type: String
},
maximpact: {
type: String
},
controleffectiveness: {
type: String
},
recommendedriskrating: {
type: String
},
frequency: {
type: String
},
impact: {
type: String
},
validatedreviewriskrating: {
type: String
},
rationalforriskadjustment: {
type: String
}
})
您可能已经注意到,我正在尝试通过以下代码更改maximpact的值:
{
"_id" : ObjectId("5d10a0a6df45c71a6c9dab26"),
"ref" : "R13", "riskstatements" : "blablablablablabla",
"maximpact" : "I AM TRYING TO CHANGE THIS VALUE",
"controleffectiveness" : "blablablablablabla",
"recommendedriskrating" : "blablablablablabla",
"frequency" : "blablablablablabla",
"impact" : "blablablablablabla",
"validatedreviewriskrating" : "blablablablablabla",
"rationalforriskadjustment" : "blablablablablabla", "__v" : 0
}
所以我虽然有效。但是,当我发送第二次查询以检索表数据时,我注意到没有任何变化:
BusinessLineDashboard.findOne({
ref: 'R13'
}, (err, rowData) => {
if (err)
console.log(err);
else {
rowData.maximpact = 'THE DATA GETS STORED IN THE DB SUCCESFULLY'
console.log('rowData: ', rowData.maximpact);
//In the console: rowData: THE DATA GETS STORED IN THE DB SUCCESFULLY
}
});
所以实际上什么都没有改变。我认为这与JS的异步特性有关。
知道为什么会发生这种情况以及可能如何解决?
谢谢!
答案 0 :(得分:0)
您必须更新与ref:'R13'相对应的BusinessLineDashboard表中的maximpact。如果不进行更新,则如何更改该值。
使用BusinessLineDashboard.update({ref:'R13'},{maximpact:'成功存储在数据库中的数据位'})