title: How to define varying attributes for a product system in mongoose (node.js)
body: <p>I’m building a personal shop app where users can sell items to each other, but I’m having a difficult time figuring out how to manage the products. For instance if you want to sell a t-shirt you should be able to pick a size and color etc. but if you sell a computer you should specify the year, cpu power etc. All products have a title, price, images and so on, but how would you get by with the varying attributes? I am using mongodb for the objects.</p>
  
  <p>I was thinking about having a field <code>attributes</code> which should be an object with the different details, and then a have field <code>type</code> that would define which properties that exists. If <code>type = 'Computer</code> then I would know that <code>attributes</code> would look something like this. </p>
  
  <pre><code>attributes: { 
      capacity: 1000 // gb
      ram: 4096 // MB  
  }
  </code></pre>
  
  <p>etc.</p>
  
  <p>In a normal object oriented design I would have done this through inheritance / interfaces. If you have any idea on the best approach for this in mongoose / node.js I would be happy to hear about it. </p>
  
  <p>If I'm not making myself clear in the question please tell me what is vague and what should be clarified</p>
  
  <p>Edit: </p>
  
  <p>The following article describes one solution to the problem
  <a href="http://learnmongodbthehardway.com/schema/chapter8/">http://learnmongodbthehardway.com/schema/chapter8/</a></p>
  
  <p>It however doesn't state where to put the attributes. One solution might be to just store it in the category itself, but I'm not sure about best practices here though.</p>