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

Storing versions in a collection rather than in an array

Open diochnos opened this issue 10 years ago • 3 comments

Dear Christoph,

I have a project where I want to keep track of all the versions of some documents in a certain collection. In order to do that I plan to modify the __v field by incrementing it by 1 and then the idea is that the plugin will take care of storing the different versions that I want to keep when I save the new version of the document in the original collection. Incrementing __v before saving the document seems to be working fine when the versions are stored in an array. However, my first concern is that Mongo files can not be larger than 16MBs and since I will be storing every single revision that is made on a document, this may mean that I will soon run into problems.

For this reason I was hoping that storing the versions in a collection would be a better idea. To my understanding this would imply different documents (i.e. with different _id's) in the collection where the different versions are stored, and then later on if I would like to inspect a past version I would be able to do so based on the refId and __v fields (where the refId is the _id of the object for which we are keeping the different versions).

However, using the same code, and only changing the strategy from 'array' to 'collection' gives me an error. Apparently, when I am attempting to save the new version of the object everything proceeds ok in the non-versions collection, but in the collection where the versions are stored I get an error in the sense that the plugin is attempting to write another object with the same _id field as the one that exists due to version 0 (i.e. due to the creation). Actually, through the mongo shell I can verify the discrepancy between the different versions of the document (the normal one is in version 1 and the collection with the versions only shows the one in version 0). Is this something expected? I doubt, or I am doing something very wrong by incrementing the __v field before saving. However, I am a complete newbie working with mongoDB and mongoose.

I would be very happy to share the code if that would help. Regarding the details of this toy example that I have I am using express 3.4.8, jade 1.3.0, mongoose 3.8.8, and mongoose-version 0.2.4. (The other thing that I can observe is that the _id that is used for the document in the collection with the different versions is the same as the refId. This appears to be a little bit odd.)

Let me know what you think.

Best regards

diochnos avatar Apr 04 '14 19:04 diochnos

@diochnos I think this is the same issue I had and submitted this PR but there are some issues with just deleting the _id from the version collection so it should be set to null instead:

This line:

delete versionedModel._id;

should be:

versionedModel._id = null;

See if that works for you. I had attempted to work on submitting this other PR and adding tests for collections and fixing some other issues, but I keep running into an issue with another test due to the version of mongoose I was using. I haven't had a chance to get back to it.

doowb avatar Apr 04 '14 22:04 doowb

@doowb Thank you Brian.

Indeed your suggestion seems to be working based on some preliminary testing. The versioned document referring to version 0 has no field called refVersion but otherwise I think I am very happy at the moment. Thanks again for your time and immediate response. Finally, if there is a way for me to help with other issues that you mention, feel free to drop me a line and I can do some testing.

diochnos avatar Apr 05 '14 12:04 diochnos

Hi there, could you please add support for ignorePaths in the collection strategy as well?

sandytrinh avatar Sep 05 '14 00:09 sandytrinh