5

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

1 Answers1

2

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?

Your assumption is correct - there is no need to store the same data of additives in each one of your products. Indeed, the right approach is to extract the additives data to a different collection, and reference the relevant additives for each product.

This way, your ProductSchema will look like this (for brevity I excluded the 'required: false' field since false is the default value):

import mongoose from "mongoose";
const ProductSchema = mongoose.Schema({
  title: {
    type: String,
    required: true,
    maxlength: 60,
  },
  desc: {
    type: String,
    maxlength: 200,
  },
  img: String,
  prices: {
    type: [Number],
    required: true,
  },
  extraOptions: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'ExtraOptions'
  }],
});

export default mongoose.models.Product || mongoose.model("Product", ProductSchema);

In this way, when you get some products documents, the 'extraOptions' property will contain the id's of the extraOptions ducements.
If you want to retrieve the data of the additives itself, instead of just the id, you can use the populate method of mongoose:

Product.find().populate('extraOptions');

For further learning about schema design in mongoDB (it might help you decide in general whether to reference or embed), check this video

Omer Setty
  • 198
  • 1
  • 2
  • 11