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

feat(document): add schemaFieldsOnly option to toObject() and toJSON() #15259

Open
wants to merge 1 commit into
base: 8.11
Choose a base branch
from

Conversation

vkarpov15
Copy link
Collaborator

Fix #15218

Summary

toObject() and toJSON() currently return an object with all fields from the document in the database, which may include fields that aren't in the schema. To work around that, we added a schemaFieldsOnly option to toObject() and toJSON(), which will loop over schema paths when getting values vs just cloning the entirety of _doc.

Examples

@vkarpov15 vkarpov15 added this to the 8.11 milestone Feb 13, 2025
Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, some minor style suggestions.

Comment on lines +219 to +220
/** If true, the resulting object will only have fields that are defined in the document's schema. By default, `toJSON()` returns all fields in the underlying document from MongoDB, including ones that are not listed in the schema. */
schemaFieldsOnly?: boolean;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** If true, the resulting object will only have fields that are defined in the document's schema. By default, `toJSON()` returns all fields in the underlying document from MongoDB, including ones that are not listed in the schema. */
schemaFieldsOnly?: boolean;
/** If true, the resulting object will only have fields that are defined in the document's schema. By default, `toJSON()` & `toObject()` returns all fields in the underlying document from MongoDB, including ones that are not listed in the schema. */
schemaFieldsOnly?: boolean;

Comment on lines +14309 to +14310
const type1Key = 'type1';
const type2Key = 'type2';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, this could also be used in the .discriminator calls

Comment on lines +14327 to +14328
assert.strictEqual(doc2.field1, undefined);
assert.strictEqual(doc2.field2, 'test2');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.strictEqual(doc2.field1, undefined);
assert.strictEqual(doc2.field2, 'test2');
assert.strictEqual(doc2.field2, 'test2');
assert.strictEqual(doc2.field1, undefined);

Align with the style of the following 2 checks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants