0

Let's consider this schema:

var elementSchema = new Schema({
  name: String,
  last_edit: { type: Date, default: Date.now }
});

Now, every time I update any element.name I would like that mongoose updates directly the last_edit time.

In Mongoose Middleware docs it says:

var schema = new Schema(..);
schema.pre('save', function (next) {
  // do stuff
  next();
});

I might update it instead of // do stuff, however the document to be saved is not passed, any hint?

1 Answer 1

3

In 'save' middleware, this is a reference to the document being saved:

schema.pre('save', function (next) {
  this.last_edit = Date.now();
  next();
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.