From 0bff4081b14d6aec93262267a0302b8b8e3ad86d Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 13 Sep 2018 09:04:46 -0400 Subject: [PATCH] test(populate): repro #6988 --- test/model.populate.test.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/model.populate.test.js b/test/model.populate.test.js index 2007b47a26b..658a2acfb97 100644 --- a/test/model.populate.test.js +++ b/test/model.populate.test.js @@ -7610,4 +7610,34 @@ describe('model: populate:', function() { 'file1.png'); }); }); + + it('handles virtual justOne if it is not set (gh-6988)', function() { + const postSchema = new Schema({ + name: String + }); + + postSchema.virtual('comments', { + ref: 'gh6988_Comment', + localField: '_id', + foreignField: 'postId' + }); + + const commentSchema = new Schema({ + postId: { type: Schema.Types.ObjectId } + }); + + const Post = db.model('gh6988_Post', postSchema); + const Comment = db.model('gh6988_Comment', commentSchema); + + return co(function*() { + const post = yield Post.create({ name: 'n1'}); + const comment = yield Comment.create({ postId: post._id }); + + const doc = yield Post.findOne({}).populate('comments').lean(); + assert.ok(Array.isArray(doc.comments)); + assert.equal(doc.comments.length, 1); + assert.equal(doc.comments[0]._id.toHexString(), + comment._id.toHexString()); + }); + }); });