-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
base: 8.11
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
/** 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** 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; |
const type1Key = 'type1'; | ||
const type2Key = 'type2'; |
There was a problem hiding this comment.
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
assert.strictEqual(doc2.field1, undefined); | ||
assert.strictEqual(doc2.field2, 'test2'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
Fix #15218
Summary
toObject()
andtoJSON()
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 aschemaFieldsOnly
option totoObject()
andtoJSON()
, which will loop over schema paths when getting values vs just cloning the entirety of_doc
.Examples