1

I want to create a products table which has information about all the household appliances like refrigerator, Air cooler, Television e.t.c. I have created schema something like this.

var productSchema = mongooseDB.Schema({
    Brand: {type: String, required: true},
    categoryId: Number,
    Color: String,
    Type: String,
    Price: Number,
    Size: Number
});

But the problem is each product has different attributes. So i cannot think of any generic schema. How would I go with this ? Or should I create a seperate table for each specific product ?

Community
  • 1
  • 1

3 Answers3

0

I believe you want to set the option strict to false when defining your schema. You should probably have some common field to identify what kind of product it is. Also take a look at discriminators -- might be useful.

Take heed however to the note: do not set to false unless you have good reason.

In terms of why mongoose uses schemas, this answer explains it well.

Mikey
  • 6,728
  • 4
  • 22
  • 45
0

You are not forced to use Mongoose at all if it doesn't fill your needs. You can just use the native MongoDB driver in your app.

Or I guess you can use Mongoose with an empty schema and specify the strict option to false.
http://mongoosejs.com/docs/guide.html#strict

TGrif
  • 5,725
  • 9
  • 31
  • 52
0

You can use discriminators to get polymorphic behaviour in mongoose. Check out my answer to this question: How to use mongoose schema when you have dynamic values?

Mustehssun Iqbal
  • 576
  • 3
  • 19