@@ -3901,14 +3901,24 @@ Document.prototype.$toObject = function(options, json) {
3901
3901
*
3902
3902
* _Note: if a transform function returns `undefined`, the return value will be ignored._
3903
3903
*
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.
3905
3906
*
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;
3908
3910
* }
3909
3911
*
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
3912
3922
*
3913
3923
* If you want to skip transformations, use `transform: false`:
3914
3924
*
0 commit comments