Thursday, 1 June 2017

MongoDB schema design for one model with a different templates

i just want to have your opinion guys, because i'm having a hard time thinking whether this schema design is good. So before i ask, here's one of the function of the website i'm building..

  1. User can post something on what they're selling
  2. User can search the posts
  3. Users view the post and it's specific details

Here's my POST Schema

var PostSchema = new mongoose.Schema({
    maincategory: String,
    subcategory: String,
    quantity: String,
    name: String,
    description: String,
    plusdetails: {}
}, {strict: false});

On the website, when you post something, you will select the main category and it's sub-category, for example i'm selling a Computer, on my PostSchema, it accepts the fields like maincategory, subcategory, quantity, name and description. Then since you selected a subcategory for computers,i have a specific template to serve specific details for Computer (like GPU, CPU, how many cores, how much ram and etc..) So that's why i have this called plusdetails on my PostSchema, that accepts a schemaless design.

And if for example i want to sell a Cellphone, then on my plusdetails, it will contain (like OS, how much ram, its CPU, Camera Pixel and etc..) Here's a preview of what it looks like (sorry this is not about cellphone or computer, but you'll get the idea)

enter image description hereenter image description here

So all in all, every different categories, when you post, they're all using one model which is PostSchema even though they contain different values (which is on the plusdetails). So when i search on the posts, i'll just use

Post.find({query: data})

because they're all residing on one model.

Now my question is, is this the right way to do this?

because at first, what i was thinking to do is in every category is equivalent to one model like for example,

  • PostCarSchema
  • PostComputerSchema
  • PostHouseSchema

and so on and so forth, it will take hundreds of models. With this, i can define their schema properly, i have a full control on what it should be there. But the problem is, when the User search something. What model would i use? Because it's just a one search bar, for example, i'll search "Lenovo", enter image description here On this approach (which is one category = one model), how will i search lenovo if i have multiple models. Because lenovo can be a computer or a mobile phone. It's because of this problem i didn't do this approach. Before i could just use Post.find({query: data}) but with this, i can't because there's hundreds of models

So how can i design my mongodb schema that can serve POST items that each of their own has a specific template? Is it alright the one that i did? or is there a better way?

Thank you so much guys!



via John

No comments:

Post a Comment