I\'m using mongoose (on node) and I\'m trying to add some additional fields to a model on save by using Mongoose middleware.
I\'m taking the often-used case of want
I know this is really old question, but i am answering it as I spend half a day trying to figure this out. We can pass extra properties as options as following example -
findOneAndUpdate({ '_id': id }, model, { **upsert: true, new: true, customUserId: userId, ipAddress: ipaddress.clientIp** }, function (err, objPersonnel) {
And in pre update and save access as following -
schema.pre('findOneAndUpdate', function (next) {
// this.options.customUserId,
// this.options.ipAddress
});
Thanks,