1

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'
            }
        }]
    }]
}
kentor
  • 16,553
  • 20
  • 86
  • 144
  • 1
    So you want every new array entry to have a revision number that is basically that array's count minus one, right? You could just fetch that document, get the history array count, increment that and add it as revision number on the history element you want to append. – guessimtoolate Jul 02 '17 at 20:34
  • @guessimtoolate I think that wouldn't work in case I want to delete a revision entry from that list. But yes I could order the list by revision number asecnding and increment the highest value. However I don't know how to properly implement this? Is there an elegant way to solve this? – kentor Jul 02 '17 at 20:53
  • Depends on the rest of your code, so I can't give you a clear answer. Just give it a go. I wonder though, why would you delete a revision? Isn't the whole point of it to preserve history? – guessimtoolate Jul 02 '17 at 21:22
  • What other code do you want to see? I don't have a starting point. Can this be done inside of the mongoose model with pre save method or what would be the preferred way to go? – kentor Jul 02 '17 at 22:54
  • 1
    Related: Your own followup question [Get most recent Sub-Document from Array](https://stackoverflow.com/a/44876620/2313887) where I concluded in response that there some problems in this design, being that you cannot really "increment" such a "version" in an atomic way. It also should be completely unnecessary, since the "latest revision" can actually be achieved by different techniques than by recording an incremented number. In short "latest" is **always** the "last", unless you specifically do something to alter that. – Neil Lunn Jul 03 '17 at 01:40

0 Answers0