Discriminator Key is tied to Model Name
Within mongoose you can only have one Model with a specific name. This is generally acceptable as you have one collection per Model. With the discriminator structure, it would be nice to allow multiple subtypes (of different discriminators) to have the same name.
An example of this would be:
Vehicle |-- Car |-- Van |-- Plane
Shape |-- Sphere |-- Plane |-- Cone
If Vehicle and Shape are both the base discriminator type, I would have two collections in mongodb of 'shapes' and 'vehicles'
If I where to run in one file: var PlaneModel = VehicleModel.discriminiator('Plane', {...});
And in another file: var PlaneModel = ShapeModel.discriminiator('Plane', {...});
I would get an error, because I am unable to have two Models with the same name.
Is there anyway to solve this with the current codebase or would it require a change?
Nope, it'll require a change. The assumption that model names are unique is very core to mongoose: lots of people write code that references models by name, that is, mongoose.model('Breakfast').find(). I don't think we'll be supporting discriminator name conflicts any time soon...
@vkarpov15 digging out this older issue as I have a related question: Would it be possible to implement a backward compatible change to switch to an alternate model naming convention for discriminated models?
I would propose a way to set the model name for discriminators to include the base name with dot notation. So in the example above one would access them by mongoose.model('Vehicle.Plane') and mongoose.model('Shape.Plane'). That way the model names are still unique but the discriminator keys can be shorter.
My use case is a graph-like structure where an edge type such as has would be useful for many edges. Book has Author, Comment has Creator, etc.
It would allow me to use shorter discriminator keys which makes a difference for the storage requirements when having millions of edges.
Seems that a change to the naming convention might also be needed for #2851 Issues like #4148 could be avoided as well
I like the 'Vehicle.Plane' idea at first glance, always found it confusing that discriminators could be accessed via top-level .model(). I'm going to open this one up for discussion and think about it some more.
Further comments, thoughts, arguments, and haikus most welcome from anyone :+1:
I like this idea, it makes more sense that way for discriminators.
Hello everyone. I need this feature, because have a lot of discriminators with the same names as "top level" models. I need to migrate to newest version of mongoose from 4.0.3 to reach new features, but unable to do this because of that strict behaviour.
However, I'm solving this through creating additional connections to DB and splitting models creation to this connections, but, still, it is a not very good practice.
Solved
- It was necessary for me to to fit our naming style to this behaviour.
- And moreover, move to Mongoose 5
- And leave all data untouched, I mean, no changes, at all, a lot of production running data.
Attempt to change Mongoose.js Discriminator Value leaving Discriminator Name untouched
For me this was solution, I don't know how it will treat if there will be other requirements.
Hello ;^) @arciisine, @zoellner, @vkarpov15
Can you answer if this is still open or not? My example is here: https://github.com/Automattic/mongoose/pull/6100#issuecomment-375109453
I can confirm this works well for me after 5.0.8 release. I feel I need make pull request to the docs, but is this solved?
Agreed with @zoellner. The discriminators should be treated as sub model instead of top model
@cuongsimplypost just read it, everything is already done: "Discriminator value no more tied to ModelName"
@wentout yeah this is correct. I'll keep this issue open because we need to add docs