mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Discriminator Key is tied to Model Name

Open arciisine opened this issue 11 years ago • 11 comments

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?

arciisine avatar Jan 14 '15 22:01 arciisine

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 avatar Jan 15 '15 20:01 vkarpov15

@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

zoellner avatar Oct 03 '17 23:10 zoellner

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.

vkarpov15 avatar Oct 04 '17 23:10 vkarpov15

Further comments, thoughts, arguments, and haikus most welcome from anyone :+1:

vkarpov15 avatar Oct 04 '17 23:10 vkarpov15

I like this idea, it makes more sense that way for discriminators.

CKarper avatar Nov 08 '17 17:11 CKarper

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.

wentout avatar Dec 20 '17 09:12 wentout

Solved

  1. It was necessary for me to to fit our naming style to this behaviour.
  2. And moreover, move to Mongoose 5
  3. 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.

wentout avatar Feb 06 '18 18:02 wentout

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?

wentout avatar Mar 23 '18 13:03 wentout

Agreed with @zoellner. The discriminators should be treated as sub model instead of top model

cuongsimplypost avatar Mar 30 '18 04:03 cuongsimplypost

@cuongsimplypost just read it, everything is already done: "Discriminator value no more tied to ModelName"

wentout avatar Mar 30 '18 08:03 wentout

@wentout yeah this is correct. I'll keep this issue open because we need to add docs

vkarpov15 avatar Apr 03 '18 21:04 vkarpov15