Skip to content

Commit cb62f08

Browse files
authored
Merge pull request #7891 from jyrkive/master
Fix #7889
2 parents d7ae2e3 + ea283b7 commit cb62f08

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/types/array.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ function MongooseArray(values, path, doc) {
3939
// TODO: replace this with `new CoreMongooseArray().concat()` when we remove
4040
// support for node 4.x and 5.x, see https://i.imgur.com/UAAHk4S.png
4141
const arr = new CoreMongooseArray();
42+
arr[arrayAtomicsSymbol] = {};
4243

4344
if (Array.isArray(values)) {
4445
values.forEach(v => { arr.push(v); });
46+
47+
arr[arrayAtomicsSymbol] = values[arrayAtomicsSymbol] || {};
4548
}
4649

4750
const keysMA = Object.keys(MongooseArray.mixin);
@@ -52,7 +55,6 @@ function MongooseArray(values, path, doc) {
5255

5356
arr[arrayPathSymbol] = path;
5457
arr.validators = [];
55-
arr[arrayAtomicsSymbol] = {};
5658
arr[arraySchemaSymbol] = void 0;
5759
if (util.inspect.custom) {
5860
arr[util.inspect.custom] = arr.inspect;

test/document.populate.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,44 @@ describe('document.populate', function() {
567567
});
568568
});
569569

570+
describe('gh-7889', function() {
571+
it('should save item added to array after populating the array', function(done) {
572+
const Car = db.model('gh-7889-1', {
573+
model: Number
574+
});
575+
576+
const Player = db.model('gh-7889-2', {
577+
cars: [{ type: Schema.Types.ObjectId, ref: 'gh-7889-1' }]
578+
});
579+
580+
let player;
581+
582+
Car.create({ model: 0 }).then(car => {
583+
return Player.create({ cars: [car._id] });
584+
}).then(() => {
585+
return Player.findOne({});
586+
}).then(p => {
587+
player = p;
588+
return player.populate('cars').execPopulate();
589+
}).then(() => {
590+
return Car.create({ model: 1 });
591+
}).then(car => {
592+
player.cars.push(car);
593+
return player.populate('cars').execPopulate();
594+
}).then(() => {
595+
return Car.create({ model: 2 });
596+
}).then(car => {
597+
player.cars.push(car);
598+
return player.save();
599+
}).then(() => {
600+
return Player.findOne({});
601+
}).then(player => {
602+
assert.equal(player.cars.length, 3);
603+
done();
604+
});
605+
});
606+
});
607+
570608
describe('depopulate', function() {
571609
it('can depopulate specific path (gh-2509)', function(done) {
572610
const Person = db.model('gh2509_1', {

0 commit comments

Comments
 (0)