Skip to content

Commit 3ee657d

Browse files
committed
docs(document): clarify that transform function option applies to subdocs
Fix #13757
1 parent 880bb2c commit 3ee657d

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

lib/document.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -3901,14 +3901,24 @@ Document.prototype.$toObject = function(options, json) {
39013901
*
39023902
* _Note: if a transform function returns `undefined`, the return value will be ignored._
39033903
*
3904-
* Transformations may also be applied inline, overridding any transform set in the options:
3904+
* Transformations may also be applied inline, overridding any transform set in the schema options.
3905+
* Any transform function specified in `toObject` options also propagates to any subdocuments.
39053906
*
3906-
* function xform (doc, ret, options) {
3907-
* return { inline: ret.name, custom: true }
3907+
* function deleteId(doc, ret, options) {
3908+
* delete ret._id;
3909+
* return ret;
39083910
* }
39093911
*
3910-
* // pass the transform as an inline option
3911-
* doc.toObject({ transform: xform }); // { inline: 'Wreck-it Ralph', custom: true }
3912+
* const schema = mongoose.Schema({ name: String, docArr: [{ name: String }] });
3913+
* const TestModel = mongoose.model('Test', schema);
3914+
*
3915+
* const doc = new TestModel({ name: 'test', docArr: [{ name: 'test' }] });
3916+
*
3917+
* // pass the transform as an inline option. Deletes `_id` property
3918+
* // from both the top-level document and the subdocument.
3919+
* const obj = doc.toObject({ transform: deleteId });
3920+
* obj._id; // undefined
3921+
* obj.docArr[0]._id; // undefined
39123922
*
39133923
* If you want to skip transformations, use `transform: false`:
39143924
*

0 commit comments

Comments
 (0)