1

I am trying to keep a track of all the revisions in some documents in a mongo database using mongoose. For this reason I decided to go with the plugin mongoose-version.

When I create a new document and add it to the database I can see that a version is kept in the collection that stores the different versions. However, when I update a document with a new one, I can see the changes being made in the collection that stores the document (the latest version), but not a new version stored in the versioned database. Any clues why this might be happening?

This is my schema:

var userSchema = new mongoose.Schema({
    name      : String,
    username  : { 'type': String, 'unique': true },
    email     : { 'type': String, 'unique': true },
    createdOn : { 'type': Date, 'default': Date.now },
    modifiedOn: { 'type': Date, 'default': Date.now }
});

mongoose.model('User', userSchema, 'users');

// Versioning options
var userVersioningOptions = {
    collection: 'versionedUsers',
    logError: true,
    suppressVersionIncrement: true,     // default value
    strategy: 'array',
    removeVersions: false               // default value
};

userSchema.plugin(version, userVersioningOptions);

This is how I create the users:

User.create({
    name      : request.body.FullName,
    username  : request.body.UserName,
    email     : request.body.Email,
    modifiedOn: Date.now()
}, function (err, user) {
    if (err) {
        console.log(err);
        if (err.code === 11000) {
            response.redirect('/users/new?exists=true');
        }
        else {
            response.send("Error while creating new user. The error code was " + err.code + " and the entire error " + err);
            //response.redirect('users/new?error=true');
        }
    }
    else {
        console.log("User created and saved: " + user);
        response.redirect('/users/' + user.username);
    }
});

And this is how I update the details of the users (essentially only the name can change, but it is for demonstration purposes as I am learning).

User.update(
    {username: newDetails.username},
    {$set: {name: newDetails.name, modifiedOn: Date.now()}},
    {upsert: false, multi: true},
    function (err, numAffected) {
        if (err)
            throw err;
        console.log("The error was " + err);
        console.log("Updated " + numAffected + " documents");
        response.send({}, 200);
    }
);

Any help or hints will be much appreciated as I am also a newbie in using mongo and mongoose as well.

MightyMouse
  • 13,208
  • 8
  • 33
  • 43
  • any luck with this one? – Desmond Morris Oct 29 '14 at 01:30
  • Yes. I did my own versioning using collections as I intended in the beginning. First, I added a property `_revision` in the documents of the collections that I wanted to version. Then in my code, I increment this value by 1 each time I want to write a new version to the db. Then, essentially I cloned the repo, and I modified/added slightly the code on the `collection.js` file (about 4-5 lines of code). The trick was to create a clone schema such that the object id would be something random (the default) and refer to the appropriate version through the `_revision` property. – MightyMouse Oct 29 '14 at 03:22
  • So, the cloned schema was almost literally a copy of the latest version of the document that I was saving. Thus, there were no real issues I had to be careful or take care of since I was not modifying the content, I was just replicating. I also made the change suggested here: https://github.com/saintedlama/mongoose-version/issues/9 – MightyMouse Oct 29 '14 at 03:25
  • If all these sound too vague let me know and I can try to compile a more meaningful answer and post it as an actual answer to the question too. – MightyMouse Oct 29 '14 at 03:26

1 Answers1

0

mongoose-version doesn't provide support for update (https://github.com/saintedlama/mongoose-version/issues/25) as of now. The reason is because of the nature of mongoose's save vs update (there were no hooks). I believe mongoose now has hooks for update but mongoose-version has not been updated for this.

An alternative you might consider is just saving all the oplogs. Depends on your use case but this is probably easiest to setup/maintain and should have much fewer edge cases of missing changes.

I can't take credit for the suggestion. It's from: http://www.askasya.com/post/bestversion/

wlingke
  • 4,699
  • 4
  • 36
  • 52