在findOneAndUpdate()的pre钩子上修改mongoose文档

Say*_*yay 4 mongoose node.js

我有一个mongoose架构

var schema = new Schema({
    id:Number
    updated_at:Date
});
Run Code Online (Sandbox Code Playgroud)

我正在使用findOneAndUpdate()这样更新此模型

model.findOneAndUpdate(
    { id: json.id },
    json,
    {
        upsert: true,
        runValidators: true
    })
    .then(() => {
        recordsUpdated++;
    })
    .catch((err) => {
        this.emit('error', err);
    });
Run Code Online (Sandbox Code Playgroud)

传入的值json不正确,我需要对其进行一些修改.我正在寻找一个预钩子进行修改.我试过了

faction.pre('findOneAndUpdate', function (next) {
   this.update({ $set: { updated_at: this.getUpdate().updated_at * 1000 } });
   next();
});
Run Code Online (Sandbox Code Playgroud)

简而言之,我想在更新数据库之前将时间戳(以秒为单位)转换为毫秒,但这不起作用.

Say*_*yay 13

盲目地扔石头后,对我有用的是

schema.pre('findOneAndUpdate', function (next) {
    this._update.updated_at *= 1000;
    next();
});
Run Code Online (Sandbox Code Playgroud)

简而言之,需要修改_update属性中存在的文档.