I am trying to implement a file revisioning for my projects, where a sample entry would look like this:
{
"_id" : ObjectId("5935a41f12f3fac949a5f925"),
"project_id" : 13,
"updated_at" : ISODate("2017-07-02T03:27:45.502Z"),
"created_at" : ISODate("2017-06-05T18:34:07.150Z"),
"owner" : ObjectId("591eea4439e1ce33b47e73c3"),
"name" : "Demo project",
"uploaded_files" :
[{
_id : ObjectId("xy"),
display_name : 'demofile.txt',
history: [{
revision: 0,
file: ObjectId("xy")
},
{
revision: 1,
file: ObjectId("xy")
}]
}]
}
My problem:
I am not sure how I can implement the auto incrementing for the revision number inside of my history's object array. The revision
number should start at 0 for each new document inside of uplodaed_files
. What would be a decent way to achieve this behaviour?
My mongoose schema (simplified):
var projectSchema = mongoose.Schema({
name: {
type: String,
trim: true,
required: [true, 'Project name is required'],
validate: projectNameValidator,
index: true
},
website: {
type: String,
trim: true,
validate: websiteValidator
},
visibility: {
type: Number,
required: [true, 'Visiblity must be specified'],
min: 0,
max: 1
},
source_language: {
type: String,
trim: true,
required: [true, 'Source language must be specified'],
validate: sourceLanguageValidator
},
uploaded_files: [{
_id: {
type: Schema.Types.ObjectId
},
display_name: {
type: String,
trim: true,
required: [true, 'Display name must be specified'],
},
history: [{
revision: {
type: Number,
required: [true, 'Revision number must be specified']
},
file: {
type: Schema.Types.ObjectId,
ref: 'File'
}
}]
}]
}