From bf1a9b492a986221f441f7276f9c944a2d733828 Mon Sep 17 00:00:00 2001 From: Felix Reinhardt Date: Mon, 17 Aug 2020 17:10:41 +0200 Subject: [PATCH] Add regression test for Issue #9336 --- test/model.populate.test.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/model.populate.test.js b/test/model.populate.test.js index ecf2a52e33e..f9055597a40 100644 --- a/test/model.populate.test.js +++ b/test/model.populate.test.js @@ -1626,6 +1626,33 @@ describe('model: populate:', function() { }); }); + it('supports `retainNullValues` while supressing _id of subdocument', function() { + const BlogPost = db.model('BlogPost', blogPostSchema); + const User = db.model('User', userSchema); + + return co(function*() { + const user = new User({ name: 'Victor Hugo' }); + yield user.save(); + const post = yield BlogPost.create({ + title: 'Notre-Dame de Paris', + fans: [] + }); + + yield BlogPost.collection.updateOne({ _id: post._id }, { + $set: { fans: [user.id] } + }); + + yield user.delete() + + const returned = yield BlogPost. + findById(post._id). + populate({ path: 'fans', select: 'name -_id', options: { retainNullValues: true } }); + + assert.equal(returned.fans.length, 1); + assert.strictEqual(returned.fans[0], null); + }); + }); + it('populating more than one array at a time', function(done) { const User = db.model('User', userSchema); const M = db.model('Test', new Schema({