I have the following schema:
const contentSchema = new Schema({
name: {
type: String,
required: true
},
type: {
type: String,
required: true
},
value: {
type: String,
required: true
}
});
const contentArraySchema = new Schema({
contentArray: [contentSchema]
});
const pageSchema = new Schema({
contents: [contentSchema]
});
I would like to have a mix of objects and arrays added to the pageSchema. If I have an array I would like to populate it with the contentSchema objects and then add that to the pageSchema contents field array. If I don't have an array I would just like to add the contentSchema object to the pageSchema contents field array.
I was just going to use the mixed schema type but I'm not sure what the best way to go about doing this would be and what effect that would have on basic database searching.
I also thought about doing something like I saw on this post Here:
pageSchema.pre('validate', function (next) {
if (type === 'array') {
//use content array schema
} else {
//use regular content schema
}
next()
});
But I'm not really sure what the best practice is for something like this. Hopefully someone has some experience with this and can help. Thanks.