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.