Mongoose pre findOneAndUpdate hook issues

后端 未结 4 740
春和景丽
春和景丽 2021-01-01 19:36

I\'m trying to update counts on a pre hook. The issue is that for some unknown reason the findOneAndUpdate hook doesn\'t have access to the document, as far as

4条回答
  •  清酒与你
    2021-01-01 19:39

    You can do smthng like that ->

    source.pre('findOneAndUpdate', function (next) {
        console.log('------------->>>>>> findOneAndUpdate: ');
        this._update.$set.objects = [];
        this._update.$set.people = [];
        this._update.$set.events =  [];
        next();
    });
    

    pay attention to _update.$set because in the context "this" will be a query. So you can easily add anything you want!

提交回复
热议问题