mongoose-history icon indicating copy to clipboard operation
mongoose-history copied to clipboard

__v versionKey gets deleted in document

Open brianmtully opened this issue 5 years ago • 5 comments

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.

brianmtully avatar Nov 29 '18 18:11 brianmtully

Can you provide an example that breaks the updates?

nassor avatar Dec 19 '18 21:12 nassor

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.

brianmtully avatar Dec 19 '18 21:12 brianmtully

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.

Ncifra avatar Dec 24 '18 14:12 Ncifra

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.

Mobasher-NetLinks avatar Jan 31 '19 05:01 Mobasher-NetLinks

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.

Ncifra avatar Jan 31 '19 08:01 Ncifra