I want that when a new document is created the versionKey (__v) field will auto-increment.
(or should I use a new field 'version' instead __v in the schema?)
example for the schema in my app :
Template.js
var mongoose = require('mongoose');
const Test = require('./Test');
var TemplateSchema = new mongoose.Schema({
platform: String,
templateRevision: String,
tests: [Test.schema]
});
TemplateSchema.pre('save', function(next) {
this.increment();
return next();
});
module.exports = mongoose.model('Template', TemplateSchema);
routes/templatesApi.js
// create new template document
router.post('/', function(req, res, next) {
Test.create(req.body, function (err, tests) {
if (err) {
console.log(err);
return next(err);
}
res.json(tests);
});
});
where and how can I insert the auto-increment operation for versionKey property here?
Edit: I tried to use .pre('save') middleware and it still not working.
am I using it right?