Skip to content

Commit f277d94

Browse files
committed
test(update): repro #8063
1 parent d19b849 commit f277d94

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

test/model.update.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3348,4 +3348,38 @@ describe('model: updateOne: ', function() {
33483348
assert.equal(docs[1].arr.length, 1);
33493349
});
33503350
});
3351+
3352+
it('update embedded discriminator path if key in $elemMatch (gh-8063)', function() {
3353+
const slideSchema = new Schema({
3354+
type: { type: String },
3355+
commonField: String
3356+
}, { discriminatorKey: 'type' });
3357+
const schema = new Schema({ slides: [slideSchema] });
3358+
3359+
const slidesSchema = schema.path('slides');
3360+
slidesSchema.discriminator('typeA', new Schema({ a: String }));
3361+
slidesSchema.discriminator('typeB', new Schema({ b: String }));
3362+
3363+
const MyModel = db.model('gh8063', schema);
3364+
return co(function*() {
3365+
const doc = yield MyModel.create({
3366+
slides: [{ type: 'typeA', a: 'oldValue1', commonField: 'oldValue2' }]
3367+
});
3368+
3369+
const filter = {
3370+
slides: { $elemMatch: { _id: doc.slides[0]._id, type: 'typeA' } }
3371+
};
3372+
const update = {
3373+
'slides.$.a': 'newValue1',
3374+
'slides.$.commonField': 'newValue2'
3375+
};
3376+
yield MyModel.updateOne(filter, update);
3377+
3378+
const updatedDoc = yield MyModel.findOne();
3379+
assert.equal(updatedDoc.slides.length, 1);
3380+
assert.equal(updatedDoc.slides[0].type, 'typeA');
3381+
assert.equal(updatedDoc.slides[0].a, 'newValue1');
3382+
assert.equal(updatedDoc.slides[0].commonField, 'newValue2');
3383+
});
3384+
});
33513385
});

0 commit comments

Comments
 (0)