eCommerce Product Custom Options Help Needed

Hi All,

New to MongoDB and building eCommerce website. I will have products and each may have unique product options.

When adding a new product, I may have many product options:

  • Product One
    • Size: 2", 4", 6" etc
    • Color: Blue, White, Yellow
  • Product two
    • Type: Sticky, Non-Sticky
    • Color: Red, White

Please suggest me a direction. The more detail, the better.

Million thanks

Hello @Lex_Semenenko, Welcome to the MongoDB Community forum!

This case is typical where there are products with some common attributes and also some unique attributes. There is a specific Design Pattern you can apply in such use case - Attribute Pattern.

You also look into the Wild Card Indexes which was introduced with MongoDB v4.2 and see how it is useful with your use case in this video: Wildcard Indexes in MongoDB 4.2 (MongoDB World 2019 Keynote, part 3).



You can model your Product Schema like this:

const productSchema = new Schema(
    name: { type: String, required: true },
    description: { type: String },
    size: { type: String },
    color: { type: [String] },
    type: { type: [String] }

You can add additional properties like this. Note that only name field will be required when creating a new product in the database. If you want to make some other property required, just add required: true to that field.