mongoose-history
mongoose-history copied to clipboard
__v versionKey gets deleted in document
When using versionKeys in schema. The versionKey gets deleted when using this package.
` function createHistoryDoc(d, operation) { d.__v = undefined;
let historyDoc = {};
historyDoc['t'] = new Date();
historyDoc['o'] = operation;
historyDoc['d'] = d;
return historyDoc;
} ` In the createHistoryDoc function, it sets the __v of the main document to undefined. This breaks updates on the document in the future. Big problem.
Can you provide an example that breaks the updates?
any call of findOneAndUpdate will run your 'pre' hook. Your hook sets the __v to undefined. After your hook runs the document is saved without the __v. So I believe an example would be calling a findOneAndUpdate and then after that is done, trying to update the document using save. The save pre hook does not have this issue of setting the __v to undefined.
Hey @nassor, I think @brianmtully is referring to the original document's __v
field. I am encountering the same problem where the original document's __v gets set to null
(not undefined
as above), but from the above I think that the history document's __v is set to undefined, not the original one.
Same issue occur when I use findOneAndUpdate
, it removes __v
value and change it to null in main collection documents, and break future update procedure. Please solve it, or let us know to find some other ways for history log.
For now I did change the default mongoose version key name to another, instead of the default __v
. It seems very strange why this one changes the __v
key considering that it should be a post hook trigger, and the document must have been already saved, and the change shouldn't persist.