From 3af51524599421f64e9630f17c1dd3ab1e8bb1e1 Mon Sep 17 00:00:00 2001 From: Aaron Heckmann Date: Mon, 11 Feb 2013 21:18:45 -0800 Subject: [PATCH] fixed; allow updating versionKey closes #1265 --- lib/model.js | 9 +++++++++ lib/schema.js | 5 +---- test/versioning.test.js | 6 ++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/model.js b/lib/model.js index 367d38d7d5..790357f317 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1951,6 +1951,15 @@ Model._getSchema = function _getSchema (path) { */ Model.compile = function compile (name, schema, collectionName, connection, base) { + var versioningEnabled = false !== schema.options.versionKey; + + if (versioningEnabled && !schema.paths[schema.options.versionKey]) { + // add versioning to top level documents only + var o = {}; + o[schema.options.versionKey] = Number; + schema.add(o); + } + // generate new class function model (doc, fields, skipId) { if (!(this instanceof model)) diff --git a/lib/schema.js b/lib/schema.js index 62d9d5c9c7..6359b4851e 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -81,10 +81,7 @@ function Schema (obj, options) { if (autoid) { this.virtual('id').get(idGetter); } - - // versioning not directly added to schema b/c we only want - // it in the top level document, not embedded ones. -}; +} /*! * Returns this documents _id cast to a string. diff --git a/test/versioning.test.js b/test/versioning.test.js index c6ddcf9453..f89cb64633 100644 --- a/test/versioning.test.js +++ b/test/versioning.test.js @@ -53,6 +53,12 @@ var BlogPost = new Schema({ mongoose.model('Versioning', BlogPost); describe('versioning', function(){ + it('is only added to parent schema (gh-1265)', function(done){ + assert.ok(BlogPost.path('__v')); + assert.ok(!BlogPost.path('comments').__v); + assert.ok(!BlogPost.path('meta.nested').__v); + done(); + }) it('works', function (done) { var db = start()