title: Couchbase data modelling - Document oriented
body: <p>This question is not necessarily Couchbase 2.0 develop preview specific however I think it may help people with investigation into the new Couchbase product.</p>
  
  <p>I am looking for advice on data modelling. We are investigating Couchbase with a view to possibly using it for Realtime Analytics.</p>
  
  <p>However I cannot find any documentation on how best to model real world data.</p>
  
  <p>I shall propose a scenario and if the community could help me or discuss some ideas for how this could be modelled it would be very useful?</p>
  
  <p><strong>Note this is not representative of our product and I am not asking people to solve our modelling for us the question is more intended for discussion</strong></p>
  
  <p>Lets assume that customers make purchases of products at a particular date/time, products have information with them such as id, name, description and price, a purchase is performed on a date. </p>
  
  <p>The initial requirement is to be able to count all purchases between two dates. For any 1 day there might be over 100,000 purchases - this is a pretty big business ;)</p>
  
  <p>If any of the syntax is incorrect please let me know - all advice/help is welcome.</p>
  
  <p>If we modelled the data something like so (which maybe completely incorrect):</p>
  
  <p><strong>Purchases with products</strong></p>
  
  <pre><code>{
      "_id" : "purchase_1",
      "_rev" : "1-1212afdd126126128ae",
      "products" :  [
          "prod_1" : {
              "name" : "Milk",
              "desc" : "Semi-skimmed 1ltr",
              "price" : "0.89"
          },
          "prod_7568" : {
              "name" : "Crisps", 
              "desc" : "Salt and Vinegar",
              "price: "0.85"
          }
      ]
      "date" : "2012-01-14 14:24:33"
  }
  
  {
      "_id" : "purchase_2",
      "_rev" : "1-1212afdd126126128ae",
      "products" :  [
          "prod_89001" : {
              "name" : "Bread", 
              "desc" : "White thick sliced",
              "price: "1.20"
          }
      ]
      "date" : "2012-01-14 15:35:59"
  }
  </code></pre>
  
  <p>So given that document layout we can see each purchase and we can see the products that were in that purchase - however how could we go about counting all the purchases between two dates? Also how could you see a log of all the purchases between two dates in date descending order?</p>
  
  <p>Is this something Couchbase is suited for?</p>
  
  <p>There might be hundreds of thousands of purchases between two dates and the customer doesn't like to wait for reports….as I'm sure everyone has experienced ;)</p>
  
  <p>Would it be best to use the incr functions and if so how would you go about modelling the data?</p>
  
  <p>Many thanks to anyone that reads this - I hope to expland on this further giving more examples of real world modelling problems if possible.</p>
  
  <p>James</p>