Feature to undo/revert changes on a document (which haven't been persisted yet)
Author: adamreisnzCreated Mar 24, 2020Updated Aug 19, 2026
Labelsnew featureenhancementplugin
Do you want to request a feature or report a bug? Feature request
What is the current behavior? There is no easy way to revert changes to a document.
What is the desired behavior? Something like:
const foo = mongoose
.model('Foo')
.find({_id});
console.log(foo); //{x: 2, y: 4}
foo.x = 6;
foo.y = 8;
console.log(foo); //{x: 6, y: 8}
foo.revert();
console.log(foo); //{x: 2, y: 4}Use case We have a use case where we update a document in preparation for saving, but then when payment fails, we don't proceed with the save. However, that document is then passed to an email to notify the user of the failure, but it will contain the modified data.
It'd be nice if there'd be an easy way to revert changes made on a document and revert back to the original values that were loaded from the database, thus preventing a re-fetch from the database or having to muck around with cloning objects.
Source: Automattic/mongoose