Does Mongoose support the Mongodb `findAndModify` method?

前端 未结 9 2143
[愿得一人]
[愿得一人] 2020-11-29 23:57

I would like to use findAndModify to atomically increment a field, using Mongoose.

However, the code below throws the error \"TypeError: Object # has no method \'fin

9条回答
  •  失恋的感觉
    2020-11-30 00:41

    Taking the above response from @furf, this is my promised solution:

    // eslint-disable-next-line func-names
    localeTypesSchema.statics.findAndModify = function (query, sort, update, opts, callback) {
        const cb = callback || (() => { });
        try {
            const result = this.collection.findAndModify(query || {}, sort || [], update || {}, opts);
            cb(null, result);
            return Promise.resolve(result);
        } catch (err) {
            cb(err);
            return Promise.reject(err);
        }
    };
    

提交回复
热议问题