diff --git a/lib/schema.js b/lib/schema.js index cbc1ffb593e..5ed761ad677 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -804,22 +804,17 @@ Schema.prototype.hasMixedParent = function(path) { */ Schema.prototype.setupTimestamp = function(timestamps) { if (timestamps) { - var paths = ['createdAt', 'updatedAt'].map(handleTimestampOption.bind(null, timestamps)); - var createdAt = paths[0]; - var updatedAt = paths[1]; - var schemaAdditions = paths.reduce(function(cur, path) { - if (path != null) { - var parts = path.split('.'); - if (this.pathType(path) === 'adhocOrUndefined') { - for (var i = 0; i < parts.length; ++i) { - cur[parts[i]] = (i < parts.length - 1 ? - cur[parts[i]] || {} : - Date); - } - } - } - return cur; - }.bind(this), {}); + var createdAt = handleTimestampOption(timestamps, 'createdAt'); + var updatedAt = handleTimestampOption(timestamps, 'updatedAt'); + var schemaAdditions = {}; + + if (updatedAt && !this.paths[updatedAt]) { + schemaAdditions[updatedAt] = Date; + } + + if (createdAt && !this.paths[createdAt]) { + schemaAdditions[createdAt] = Date; + } this.add(schemaAdditions); diff --git a/test/document.test.js b/test/document.test.js index f95a96f7774..905bff70e42 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -3801,7 +3801,7 @@ describe('document', function() { }); it('timestamps with nested paths (gh-5051)', function(done) { - var schema = new Schema({ props: Object }, { + var schema = new Schema({ props: {} }, { timestamps: { createdAt: 'props.createdAt', updatedAt: 'props.updatedAt'