猫鼬更新预挂钩 - 额外的数据库写入

Mongoose update pre hook - extra database writes

我正在考虑使用全局插件向 mongoose 中的所有模式添加预保存更新挂钩。

The manual recommends 在 pre-hook 中放置一个单独的更新命令,因为 this 指的是文档的查询插入:

schema.pre('update', function() {
  this.update({},{ $set: { updatedAt: new Date() } });
});

上面的代码是否会导致额外的数据库写入?此方法是否为调用 update 创建单独的数据库更新并为挂钩 update 创建另一个写入?

根据updatesource code and doc,此代码

this.update({},{ $set: { updatedAt: new Date() } });

属于

update(criteria, doc)

它不会执行,它只会在 update 发生之前将 $set: { updatedAt: new Date() } 添加到 update operation

Does the code above cause extra database writes?

没有,

Does this method create a separate db update for the calling update and another write for the hook update?

没有,