In a simple test scenario, I have a Performer and a PerformerType model.
My models are the following, for the Performer:
export interface IPerformer {
name: string;
pictures: Array<string>;
performerType: Types.ObjectId
}
export interface IPerformerSaveRequest{
name: string;
pictures: Array<string>;
performerTypeKey: number
}
const PerformerSchema: Schema = new Schema({
name: { type: String, required: true },
pictures: { type: Array<String>, required: false },
performerType: { type: Types.ObjectId, ref: 'PerformerType' }
});
And for the PerformerType:
export interface IPerformerType {
key: number;
value: string;
}
const PerformerTypeSchema: Schema = new Schema({
key: { type: Number, required: true, unique: true},
value: { type: String, required: true }
});
When I insert a new performer I would like to reference the type through a key rather that its objectId, that’s why I made IPerformerSaveRequest with a key.
I would like to understand the most correct and performant way to do this in MongoDB with mongoose (Express js with typescript, not that it matters), in SQL I would make a classic insert with a join statement to get the Id from the PerformerType table, what’s the equivalent here?
I want to emphasize that I would really like to understand the correct way to do it, not just a way, I know I could retrieve the values and assign them with some map function, but I guess it’s not the right way to do it.
Thanks.