Skip to content

Commit

Permalink
test(document): repro #5800
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Nov 20, 2017
1 parent 80731fd commit 93ea193
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4953,6 +4953,45 @@ describe('document', function() {
});
});

it('modifying unselected nested object (gh-5800)', function() {
var MainSchema = new mongoose.Schema({
a: {
b: {type: String, default: 'some default'},
c: {type: Number, default: 0},
d: {type: String}
},
e: {type: String}
});

MainSchema.pre('save', function(next) {
if (this.isModified()) {
this.set('a.c', 100, Number);
}
next();
});

var Main = db.model('gh5800', MainSchema);

var doc = { a: { b: 'not the default', d: 'some value' }, e: 'e' };
return Main.create(doc).
then(function(doc) {
assert.equal(doc.a.b, 'not the default');
assert.equal(doc.a.d, 'some value');
return Main.findOne().select('e');
}).
then(function(doc) {
doc.e = 'e modified';
return doc.save();
}).
then(function() {
return Main.findOne();
}).
then(function(doc) {
assert.equal(doc.a.b, 'not the default');
assert.equal(doc.a.d, 'some value');
});
});

it('consistent context for nested docs (gh-5347)', function(done) {
var contexts = [];
var childSchema = new mongoose.Schema({
Expand Down

0 comments on commit 93ea193

Please sign in to comment.