How to set extra information in mongo pre-save

Viewed 59

I want to set the creator of an entity automatically from the MongoDB schema.

I need to know how I can set the user data in the pre-save method. My code is the following:

RoleSchema.pre("save", async function (done) {
  if(!this.isNew){
    this.set('modified_at', new Date());
    this.set('modified_by', this._id);
  } else {
    this.set('created_at', new Date());
    this.set('created_by', this._id);
  }
  done();
});

Where this._id is the id of the role, but my idea is to automatically set the _id of the user who is doing the action. Is there a way I can send a params to the pre save method? Or how do you think is the best aproach to do this?

0 Answers
Related