对此有点绝望。代码通过哈希查找用户。在返回的模型上,我更改了一些值并保存。 db中的所有属性都会更改,但parent.tree_data.total_children.level的数组项[0]保持不变,尽管在执行脚本时它在模型中发生了更改,但是在db中它不会更改。有什么想法吗?
var parent = await utils.findUser(hash); //returns mongoose model
... code in between where I use values from the parent doc ...
//updating the parent doc via the model
parent.tree_data.parent_tag = "dev";
parent.tree_data.total_children.level[0]++;
parent.tree_data.direct_children.push({share_hash: result.share_hash, nicname: result.nicname});
parent.val = parent.val + 0.4;
parent.save(function(err){
if (err){
console.log(err);
}
});
tree_data的scheema部分如下所示:
tree_data: {
parent_tag: {type: String},
parent_share_hash: {type: String, required: false, trim: true},
direct_children: [{share_hash: {type: String, required: true, trim: true}, nicname: {type: "string", trim: true} }],
total_children: {level: [Number]},
},
答案 0 :(得分:1)
您需要在元素上调用markModified
,因为数组(或对象,混合类型等)在内容更改时不会自动标记。
查看Unable to update mongoose model
parent.tree_data.parent_tag = "dev";
parent.tree_data.total_children.level[0]++;
parent.tree_data.direct_children.push({share_hash: result.share_hash, nicname: result.nicname});
parent.val = parent.val + 0.4;
parent.markModified('tree_data.direct_children'); // if that doesnt work, try: paremt.markModified('tree_data')
parent.save(function(err){
if (err){
console.log(err);
}
});