Hello I need to inherit my schemas in mongoose library. Are there complete plugins for that? Or how should I do that myself?
I need to inherit all pre, post, init middleware from a Base schema also.
Discriminators are a schema inheritance mechanism. They enable you to have multiple models with overlapping schemas on top of the same underlying MongoDB collection. Suppose you wanted to track different types of events in a single collection.
Mongoose Schema vs. Model. A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.
In MongoDB, all documents are unique because of the _id field or path that MongoDB uses to automatically create a new document. For this reason, finding a document is easy with Mongoose. To find a document using its _id field, we use the findById() function.
Mongoose Schematype is a configuration for the Mongoose model. Before creating a model, we always need to create a Schema. The SchemaType specifies what type of object is required at the given path. If the object doesn't match, it throws an error.
For others looking for this functionality, Mongoose 3.8 now has Schema Inheritance via Discriminator functionality:
https://github.com/LearnBoost/mongoose/pull/1647
You may want to look at using a Mongoose Plugin:
A plugin that does what you want 'out of the box' is here:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With