The discriminatorKey
is not automatically filled. This means that the kind
field cannot be found on the saved document.
Database Module
@Global()
@Module({
imports: [
MongooseModule.forFeature([
{
name: User.name,
schema: UserSchema,
discriminators: [
{ name: Fan.name, schema: FanSchema },
],
},
]),
],
exports: [MongooseModule],
})
export class DatabaseModule {}
User Schema
@Schema({ discriminatorKey: 'kind' })
export class User {
@Prop({ enum: [Fan.name], required: true })
kind: string;
@Prop()
displayName: string;
}
export const UserSchema = SchemaFactory.createForClass(User);
Fan Schema
@Schema()
export class Fan {
kind: string;
displayName: string;
}
export const FanSchema = SchemaFactory.createForClass(Fan);
Save a Document
const userObj = new Fan();
userObj.displayName = "Sam";
return this.userModel.create(userObj);