Skip to content

Commit

Permalink
Merge pull request #9337 from FelixRe0/master
Browse files Browse the repository at this point in the history
Fix: Retaining null value for populated documents when _id is suppressed
  • Loading branch information
vkarpov15 committed Aug 22, 2020
2 parents b1bf6f0 + 01b48b5 commit 9bdb820
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/helpers/populate/assignVals.js
Expand Up @@ -243,7 +243,7 @@ function valueFilter(val, assignmentOpts, populateOptions) {
*/

function maybeRemoveId(subdoc, assignmentOpts) {
if (assignmentOpts.excludeId) {
if (subdoc != null && assignmentOpts.excludeId) {
if (typeof subdoc.$__setValue === 'function') {
delete subdoc._doc._id;
} else {
Expand Down
27 changes: 27 additions & 0 deletions test/model.populate.test.js
Expand Up @@ -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({
Expand Down

0 comments on commit 9bdb820

Please sign in to comment.