Mongodb Node.js Mongoose:值不代表设置的数据,但数据库已更改
当我调用findOneAndUpdate并将Mongodb Node.js Mongoose:值不代表设置的数据,但数据库已更改,mongodb,mongoose,Mongodb,Mongoose,当我调用findOneAndUpdate并将shared.warningMessage设置为“”时,我可以看到数据库已得到更改,但当我注销console.log(saved.shared.warningMessage)时未反映更改 那么我需要再次调用数据库来获取设置值吗?或者,是否有某种方法可以在设置设定值的同时恢复设定值 const saved = await userModel .findOneAndUpdate( { 'email.confi
shared.warningMessage
设置为“”时,我可以看到数据库已得到更改,但当我注销console.log(saved.shared.warningMessage)时代码>未反映更改
那么我需要再次调用数据库来获取设置值吗?或者,是否有某种方法可以在设置设定值的同时恢复设定值
const saved = await userModel
.findOneAndUpdate(
{ 'email.confirmationCode': confirmationCode },
{
$set: { 'shared.warningMessage': '', 'email.confirmed': true },
$unset: { 'email.confirmationCode': '' },
}
)
.exec();
if (!saved) throw new Error('Invalid confirmation code');
console.log(saved.shared.warningMessage);
我不想删除共享的的键和值。警告消息
我只希望它没有值,因此“”您需要在选项中指定新建
属性,告诉mongoose在更新完成后返回文档:
const saved = await userModel
.findOneAndUpdate(
{ 'email.confirmationCode': confirmationCode },
{
$set: { 'shared.warningMessage': '', 'email.confirmed': true },
$unset: { 'email.confirmationCode': '' },
},
{
new: true // this will return the doc after the update was done
}
)
.exec();
if (!saved) throw new Error('Invalid confirmation code');
console.log(saved.shared.warningMessage);
根据文件:
new:bool-如果为true,则返回修改后的文档,而不是
起初的默认值为false(在4.0中更改)