I'll start with this that I'm new to backend and I was looking for some solutions for my problem but i don't know which solution will be right for my problem. So to the point. I'm creating a pizza restaurant project in Next.js with Mongoose and simple api. I have for example collections : Product (here will be my pizzas) and the code for this model is below.
import mongoose from "mongoose";
const ProductSchema = new mongoose.Schema
{
title: {
type: String,
required: true,
maxlength: 60,
},
desc: {
type: String,
required: false,
maxlength: 200,
},
img: {
type: String,
required: false,
},
prices: {
type: [Number],
required: true,
},
extraOptions: {
type: [
{
text: { type: String, required: true },
price: { type: Number, required: true },
},
],
},},); export default mongoose.models.Product || mongoose.model("Product", ProductSchema);
In this Schema i have an array of extraOptions ( for example extra cheese for 1$ and extra onion for 1$) but I want to adopt the principle that all products can have the same additional additives. It is a pity to prescribe the same additives for each products ( each pizza) So, can I create a new model of extraOptions for pizzas and create some 'reference' (like in SQL but in Mongo) for collections Product? For example my simple model of extraOptions :
import mongoose from "mongoose";
const extraSchema = new mongoose.Schema(
{
title: {
type: String,
required: true,
maxlength: 60,
},
price: {
type: Number,
required: true,
},
},
);
export default mongoose.models.ExtraOptions || mongoose.model("ExtraOptions", extraSchema);
How could I create a maybe reference in the products collection to display there all documents of the additional extra options ? I was reading about CopyTo method, populate method and subdocuments but i don't know which is solution for me and how can i Use it... Thanks for all answers and sorry if i wrote epic here.
Maybe some extraOptions: [{type:mongoose.Schema.Types.ObjectId,ref:'extraSchema'}],
or i dont really know. Thanks a lot for help