I'm new with nestjs. I use @nestjs/mongoose
and I need to reference several fields in a nested object in my class schema and I don't know how to do it.
The dietDays
object must contain a date field and meals object that contain a 2 references to Meal
schema.
What is the correct way to do it?
The code below shows how I tried to do it, as well as, the other way I tried was that create dietDays
class and pass it to Prop type variable but in that scenario I am not able to reference to Meal
schema because that was not a schema.
@Schema()
export class Diet {
@Prop({ default: ObjectID })
_id: ObjectID
@Prop()
dietDays: [
{
date: string
meals: {
breakfast: { type: Types.ObjectId; ref: 'Meal' }
lunch: { type: Types.ObjectId; ref: 'Meal' }
}
},
]
}
You should do it as following:
Create a class which refers to each day in the diet ( logically make sense )
Knowing that each of which
breakfast
andlunch
should be a valid mongo schemas.If
breakfast
andlunch
are not schemas, and you have a list of content you can pass this array as possible options for them inside the schema object.Another possible way
simple note you are not required to make _id a prop of any schema
Edit
For the diet schema