[MongoDB]:编辑一个值而不重置另一个值



我有一个对象:

Options: {
Obj_1: {
Str_1: 'string'
},
Obj_2: {
Str_2: 'string'
}
}

我尝试在Obj_1内部编辑Str_1,方法是:

const edit = { $set: { Options: { Obj_1: { Str_1: 'new value!' } } } }
await <database>.findOneAndUpdate(<queue>, edit, {new:true})

但这就去掉了Obj_2,有人知道解决方案吗?

尝试这个

const Options = {
Obj_1: {
Str_1: "string",
},
Obj_2: {
Str_2: "string",
},
};
const edit = {
$set: {
Options: {
...Options,
Obj_1: { Str_1: "new value!" },
},
},
};
console.log({ edit });

最新更新