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: set default for dotted path projection #11293

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ function $__applyDefaults(doc, fields, exclude, hasIncludedChildren, isBeforeSet
break;
}
} else if (exclude === false && fields && !included) {
if (curPath in fields) {
if (curPath in fields || type.$isSingleNested && hasIncludedChildren[curPath]) {
included = true;
} else if (!hasIncludedChildren[curPath]) {
break;
Expand Down
62 changes: 62 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10998,4 +10998,66 @@ describe('document', function() {
assert.equal(typeof doc.somethingElse, 'string');
delete String.type;
});

it('applies subdocument defaults when projecting dotted subdocument fields', async function() {
const grandChildSchema = new mongoose.Schema({
name: {
type: mongoose.Schema.Types.String,
default: () => 'grandchild'
}
});

const childSchema = new mongoose.Schema({
name: {
type: mongoose.Schema.Types.String,
default: () => 'child'
},
grandChild: {
type: grandChildSchema,
default: () => ({})
}
});

const parentSchema = new mongoose.Schema({
name: mongoose.Schema.Types.String,
child: {
type: childSchema,
default: () => ({})
}
});

const ParentModel = db.model('Parent', parentSchema);
// insert an object without mongoose adding missing defaults
const result = await db.collection('Parent').insertOne({ name: 'parent' });

// ensure that the defaults are populated when no projections are used
const doc = await ParentModel.findById(result.insertedId).exec();
assert.equal(doc.name, 'parent');
assert.equal(doc.child.name, 'child');
assert.equal(doc.child.grandChild.name, 'grandchild');

// ensure that defaults are populated when using an object projection
const projectedDoc = await ParentModel.findById(result.insertedId, {
name: 1,
child: {
name: 1,
grandChild: {
name: 1
}
}
}).exec();
assert.equal(projectedDoc.name, 'parent');
assert.equal(projectedDoc.child.name, 'child');
assert.equal(projectedDoc.child.grandChild.name, 'grandchild');

// ensure that defaults are populated when using dotted path projections
const dottedProjectedDoc = await ParentModel.findById(result.insertedId, {
name: 1,
'child.name': 1,
'child.grandChild.name': 1
}).exec();
assert.equal(dottedProjectedDoc.name, 'parent');
assert.equal(dottedProjectedDoc.child.name, 'child');
assert.equal(dottedProjectedDoc.child.grandChild.name, 'grandchild');
});
});