Need suggestion in creating custom product schema

Hey developer i am pretty new to mongodb need your help guys shall be thankfull to you!
Problem is that i am creating hair wig website there is product order page where the user have choice to select multiple options if if any of option does not match to product that option will remain off
i had created a schema according to my approach need suggestion
**Hint *** one product has hundred variants like some product has full options and some has not
i am sharing my order page image that what i want to accomplished and also the that i had created

Schema

const productSchema= mongoose.Schema({
    title:{
        type:String,
        required:[true,"Enter product title"]
    },
    description:{
      type:String,
      required:[true,"Enter Product description"]
    },
    StretchedLength:[
        {
            length:{
                type:String
            },
            quantity:{
                type:Number
            }
        }
    ],
    Density:[
        {
            amount:{
                type:Number
            },
            quantity:{
                type:Number
            }
        }
    ],
    HairColor:[
        {
            name:{
                type:String
            },
            quantity:{
                type:Number
            }
        }
    ],
    shipFrom:[
        {
            location:{
                type:String
            },
            available:{
                type:Boolean,
                default:false
            }
        }
    ],
    price:{
        type:Number
        },
    createdAt:{
        type:Date,
        default:Date.now
        },
    sale:{
        status:{
        type:Boolean,
        default:false
        },
        percent:{
            type:Number,
            default:0
        }
    },
    quantity:{
        type:Number
    }
})

Order page

Hello @Nauman_Arshad, welcome to the MongoDB community forum!

You can try this approach, and it has the same details with some structural differences. I have used the wig’s attributes as an array. You can include other properties for different wigs as needed:

product: {
  title: "Wig A",
  description: "Wig A's description",
  attributes: [
    { 
        name: "Stretched Length", 
        details: [ 
            { value: "14 inches", quantity: 10 }, 
            { value: "16 inches", quantity: 6 }, 
            // ... 
        ]
    },
    { 
        name: "Density", 
        details: [ 
            { value: "200 density", quantity: 8 },
            // ... 
        ]
    },
    //
    // ... other attributes, like HairColor, etc.
  ],
  shipFrom: [
    { location: "abc", available: true },
    // ... other locations
  ],
  price: 
  quantity:
  //... other properties
}
1 Like

wow such a quick response very very thankful to you :slight_smile: that what i want now i am going to work with this structure hope so it will resolve my problem once again thank you (Y)

1 Like

@Prasad_Saya one more suggestion please this is the general schema for product what to do if there are more 100 variants with this product like the product with length 18 and density 150 cannot be ship from china same like some products are not available with particular Color
i am asking that i want to add manually all variants in the database
assume that i don’t have admin page i just adding data using post request in database


check with 150 density i dont have product with 22,20 length
i have to add variants manually in db or we can handle it using the attribute in product schema?
@Prasad_Saya

A possibility is that you can store all the possible combinations, like specify the attributes as shown in my earlier post. And, the available combinations as a separate field (and can be updated as and when there are changes). So, at any given point you can know the availability of a product with certain attributes.

{
  "title":"200% Water Wave Lace Front Wigs For Women Pre Plucked With Baby Hair Curly Human Hair Wigs Deep Wave Frontal Wigs Lace Closure",
  "description":"Brand Name: CEXXY Texture: Jerry CurlHair Grade: Remy Hair Origin: CN(Origin) Human Hair Type: Brazilian Hair Cap Size: Average Size Base Material: Swiss Lace Lace Color: Medium Brown Suitable Dying Colors: Darker Color Only Wig Material: 100% Natural Unprocessing Human Hair Wig Type 1: 4x4 Lace Closure Wig Wig Type 2: 13x4 Lace Frontal Wig Wig Length: 8inch -38inch Human Hair Wig Texture: Water Wave Deep Curly Human Hair Wig Made Method: Half Machine Made & Half Hand Tie Hairline: Pre Plucked Hairline With Baby Hair",
  "attributes":[
    {
      "name":"Stretched Length",
      "details":[
        {"value":14,"quantity":8},
        {"value":16,"quantity":8},
        {"value":38,"quantity":8},
        {"value":36,"quantity":8},
        {"value":34,"quantity":8},
        {"value":32,"quantity":8},
        {"value":30,"quantity":8},
        {"value":28,"quantity":8},
        {"value":26,"quantity":8},
        {"value":24,"quantity":7},
        {"value":22,"quantity":6},
        {"value":20,"quantity":7},
        {"value":18,"quantity":8}
      ]
    },
    {
      "name":"Density",
      "details":[
        {
          "value":200,"quantity":52
        },
        {
          "value":150,"quantity":48
        } 
      ]

    },
    {
      "name":"Hair Color",
      "details":[
        {
          "value":"4x4 Lace Closure Wig","quantity":52
        },
        {
          "value":"13x4 Lace Front Wig","quantity":48
        }
      ]

    }
  ],

  "shipFrom":[
    {
      "value":"France",
      "available":true
    },
    {
      "location":"China",
      "available":true
    },
    {
      "location":"United States",
      "available":true
    }
  ],
  "price":74.10,
  "createdAt":"",
  "sale":{
    "status":false,
    "percent":0
  }

}

how the combinations filed will be look like in this schema please give some idea please

I see this is one way to start working about the available combinations, for example:

combinations: [
    [ 
        { name: "Stretched Length", value: "14 inches" },
        { name: "Density" value: "200 density" },
        { name: "HairColor" value: "4x4 Lace Closure Wig" },
    ],
    [ 
        { name: "Stretched Length", value: "18 inches" },
        { name: "Density" value: "180 density" },
        { name: "HairColor" value: "4x4 Lace Closure Wig" },
    ],
    // ... more
]

At this point I cannot for sure know how it works in your case certainly. This was an idea I had at that moment when I wrote the comments.

Design is a process and based upon your requirements (your queries and how you use them in your app, for example) it can (and will) evolve. Its a very involving process. One needs to be intimately familiar with various aspects in an app to get the design right. As you build the app, the design can (and will) change, and be prepared for that.

@Prasad_Saya thank you for you response i agreed to your point according to my design i am adding bool value for attributes in combinations too to predict whether a product include which attributes or not so i can dynamically make that field un-select accordingly

1 Like

@Prasad_Saya sorry to disturb you again can you please tell me why the checks array inside the combinations array in not creating in database is there any issue with syntax? also the HairColor value is also empty.


Database