Skip to content

Commit 3f7dfc5

Browse files
committedSep 2, 2021
fix(document): make depopulate() handle populated paths underneath document arrays
Fix #10592
1 parent b34d1d5 commit 3f7dfc5

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed
 

‎lib/document.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,8 @@ Document.prototype.$set = function $set(path, val, type, options) {
13681368
this.unmarkModified(path);
13691369
}
13701370
}
1371+
} else {
1372+
console.log('No shouldSet', path)
13711373
}
13721374

13731375
if (schema.$isSingleNested && (this.isDirectModified(path) || val == null)) {
@@ -4203,7 +4205,7 @@ Document.prototype.depopulate = function(path) {
42034205
continue;
42044206
}
42054207
delete populated[key];
4206-
this.$set(key, populatedIds);
4208+
utils.setValue(key, populatedIds, this._doc);
42074209
}
42084210
return this;
42094211
}
@@ -4216,7 +4218,7 @@ Document.prototype.depopulate = function(path) {
42164218
delete this.$$populatedVirtuals[singlePath];
42174219
delete this._doc[singlePath];
42184220
} else if (populatedIds) {
4219-
this.$set(singlePath, populatedIds);
4221+
utils.setValue(singlePath, populatedIds, this._doc);
42204222
}
42214223
}
42224224
return this;

‎test/document.test.js

+44
Original file line numberDiff line numberDiff line change
@@ -10484,4 +10484,48 @@ describe('document', function() {
1048410484

1048510485
assert.ok(!doc.nested.otherProp);
1048610486
});
10487+
10488+
it('depopulate all should depopulate nested array population (gh-10592)', function() {
10489+
10490+
const Person = db.model('Person', {
10491+
name: String
10492+
});
10493+
10494+
const Band = db.model('Band', {
10495+
name: String,
10496+
members: [{ type: Schema.Types.ObjectId, ref: 'Person' }],
10497+
lead: { type: Schema.Types.ObjectId, ref: 'Person' },
10498+
embeddedMembers: [{
10499+
active: Boolean,
10500+
member: {
10501+
type: Schema.Types.ObjectId, ref: 'Person'
10502+
}
10503+
}]
10504+
});
10505+
10506+
return co(function*() {
10507+
const people = [{ name: 'Axl Rose' }, { name: 'Slash' }];
10508+
10509+
const docs = yield Person.create(people);
10510+
let band = {
10511+
name: 'Guns N\' Roses',
10512+
members: [docs[0]._id, docs[1]],
10513+
lead: docs[0]._id,
10514+
embeddedMembers: [{ active: true, member: docs[0]._id }, { active: false, member: docs[1]._id }]
10515+
};
10516+
band = yield Band.create(band);
10517+
yield band.populate('members lead').populate('embeddedMembers.member').execPopulate();
10518+
assert.ok(band.populated('members'));
10519+
assert.ok(band.populated('lead'));
10520+
assert.ok(band.populated('embeddedMembers.member'));
10521+
assert.equal(band.members[0].name, 'Axl Rose');
10522+
assert.equal(band.embeddedMembers[0].member.name, 'Axl Rose');
10523+
band.depopulate();
10524+
10525+
assert.ok(!band.populated('members'));
10526+
assert.ok(!band.populated('lead'));
10527+
assert.ok(!band.populated('embeddedMembers.member'));
10528+
assert.ok(!band.embeddedMembers[0].member.name);
10529+
});
10530+
});
1048710531
});

0 commit comments

Comments
 (0)
Please sign in to comment.