-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Finding discriminators should select correct model #6701
Comments
These Stack Overflow questions are relevant: How to get a list of available Mongoose Discriminators? |
I've come up with a reasonably compact way of doing this, as long as none of the documents are stored under the parent model:
|
Thanks for reporting, will fix this 👍 |
Is this in the plans anymore? I think this is really important especially people who wants to introduce Discriminators later into their development process. |
This looks fixed in 5.5.x, I believe this was fixed in 5.4.20 with #7586. The below script works for me, so I'm going to close this for now. @maloguertin can you confirm your version of Mongoose? const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);
const GITHUB_ISSUE = `gh6701`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const { Schema } = mongoose;
run().then(() => console.log('done')).catch(error => console.error(error.stack));
async function run() {
await mongoose.connect(connectionString);
await mongoose.connection.dropDatabase();
const membershipSchema = mongoose.Schema({
name: { type: String, required: true },
price: { type: Number, required: true, get: p => `£${p}.00` }
}, { discriminatorKey: 'type' });
const subscriptionSchema = mongoose.Schema({
frequency: {
type: String, enum: [ 'week' , 'month' ],
required: true, get: val => `${val}ly subscription` }
})
const ticketSchema = mongoose.Schema({
frequency: { type: String, default: 'one-off' }
});
const Membership = mongoose.model('Membership', membershipSchema);
const Subscription = Membership.discriminator('Subscription', subscriptionSchema);
const Ticket = Membership.discriminator('Ticket', ticketSchema);
await Subscription.create({ name: 'Monthly subscription', price: 1, frequency: 'month'});
await Ticket.create({ name: 'Ticket', price: 2 });
const sub = await Membership.findOne({ price: 1 });
console.log(sub);
console.log(sub.frequency); // "monthly subscription"
} |
@vkarpov15 everything works fine, I'm sorry one of my model had the discriminator key defined in a child schema instead of the parent schema. |
I want to request a feature.
Models should 'know' about their discriminator children, and select the correct schema when running
ParentModel.find()
.In the following example,
Membership
can be of two discriminator types,Subscription
andTicket
.Each of these discriminators has different
get
s, as follows:Currently, if I
find
all theMemberships
, thefrequency
getter for aSubscription
is not run.Instead I get an object like:
This happens because the documents are returned with
membershipSchema
not withsubscriptionSchema
.But given that the
__t
field is correctly being set to "Subscription", it seems reasonable that Mongoose would find the correct schema and apply it.node version: v9.10.0, mongoose version 5.2.0.
Here is a possible test:
The text was updated successfully, but these errors were encountered: