Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Retaining null value for populated documents when _id is suppressed #9337

Merged
merged 3 commits into from Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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