diff --git a/docs/discriminators.md b/docs/discriminators.md index 2507c0aa417..1e27a0809f1 100644 --- a/docs/discriminators.md +++ b/docs/discriminators.md @@ -53,3 +53,27 @@ To update a document's discriminator key, use `findOneAndUpdate()` or `updateOne ```acquit [require:use overwriteDiscriminatorKey to change discriminator key] ``` + +## Embedded discriminators in arrays + +You can also define discriminators on embedded document arrays. +Embedded discriminators are different because the different discriminator types are stored in the same document array (within a document) rather than the same collection. +In other words, embedded discriminators let you store subdocuments matching different schemas in the same array. + +As a general best practice, make sure you declare any hooks on your schemas **before** you use them. +You should **not** call `pre()` or `post()` after calling `discriminator()`. + +```acquit +[require:Embedded discriminators in arrays] +``` + +## Single nested discriminators + +You can also define discriminators on single nested subdocuments, similar to how you can define discriminators on arrays of subdocuments. + +As a general best practice, make sure you declare any hooks on your schemas **before** you use them. +You should **not** call `pre()` or `post()` after calling `discriminator()`. + +```acquit +[require:Single nested discriminators] +``` \ No newline at end of file diff --git a/test/docs/discriminators.test.js b/test/docs/discriminators.test.js index 28bb7f77ec3..f593f814ed7 100644 --- a/test/docs/discriminators.test.js +++ b/test/docs/discriminators.test.js @@ -273,6 +273,7 @@ describe('discriminator docs', function() { const batchSchema = new Schema({ events: [eventSchema] }); // `batchSchema.path('events')` gets the mongoose `DocumentArray` + // For TypeScript, use `schema.path('events')` const docArray = batchSchema.path('events'); // The `events` array can contain 2 different types of events, a @@ -391,6 +392,7 @@ describe('discriminator docs', function() { const shapeSchema = Schema({ name: String }, { discriminatorKey: 'kind' }); const schema = Schema({ shape: shapeSchema }); + // For TypeScript, use `schema.path('shape').discriminator(...)` schema.path('shape').discriminator('Circle', Schema({ radius: String })); schema.path('shape').discriminator('Square', Schema({ side: Number }));