I have a User schema with a path email. I want track changes over this field, so, when the user changes his email, a job is executed. Which is the way to do it with mongoose, since the update hooks pass a Query instead a Document (can't use isModified(path))?
Actually, instead of use Model.update, I follow this pattern:
user.email = data.email || user.email)save method.So, with this way I'm able to get the document inside the pre save hook and I can check if the path was changed via doc.isModified('email').
Would be great if docs are availabe on update and remove hooks too. It is possible?
You can access getUpdate() in a pre update hook to check whether email is getting updated or not:
mySchema.pre('update', function() {
if (this.getUpdate().email) {
// Run job
}
});
You can't use isModified(), but email won't change unless email is in the update
Most helpful comment
You can access
getUpdate()in a pre update hook to check whether email is getting updated or not:You can't use
isModified(), butemailwon't change unlessemailis in the update