title: How to model daily deltas in MongoDB
body: <p>I have some stock price data I want to model in MongoDB. Logically each stock price has a date, stock name, and price associated with it. One of the main aims of the application is to model the daily change in stock price.</p>
  
  <p>I have two questions associated with this problem.</p>
  
  <p>1) What would be the best way to model this sort of data in MongoDB given the sort of queries I'll be running on it.</p>
  
  <p>UPDATE!!! I've done a bit of experimentation and have decided to denormalise the data and make sure I have appropriate column indexes. I didn't actually see any performance difference in nesting the data for the type of queries I was running, and it complicated the process of writing aggregation routines. Thanks for the advice Raxit! </p>
  
  <p>2) Given 1) What is the best way to write these daily change style queries in MongoDB?</p>
  
  <hr>
  
  <p>So after now I have one document per date and stock.</p>
  
  <pre><code>{
      {"stock":"Bob",
       "date":"2012-12-01",
       "price":99.99                         
      },
      {"stock":"Bob",
       "date":"2012-12-02",
       "price":99.99                         
      },
      {"stock":"Bob",
       "date":"2012-12-03",
       "price":99.99                         
      },
      {"stock":"Ted",
       "date":"2012-12-01",
       "price":99.99                         
      },
      {"stock":"Ted",
       "date":"2012-12-02",
       "price":99.99                         
      },
      {"stock":"Ted",
       "date":"2012-12-03",
       "price":99.99                         
      },
      .
      .
      .
  
  
  }
  </code></pre>
  
  <p>My thoughts on 2:</p>
  
  <p>I want to generate reports that display the daily change in stock prices. Given Document size limitations I may need to split this up to be the deltas per stock and per year or something but that is more of an implementation detail. Essentially I want to output documents of the form:</p>
  
  <pre><code>{
   {"stock":"Bob",
            "prices": {[{"fromDate":"2012-12-01","toDate":"2012-12-02","delta":-1.0},
                       {"fromDate":"2012-12-02","toDate":"2012-12-03","delta":-1.0},
                       .
                       .
                       .]}
   },
   {"stock":"Ted",
            "prices": {[{"fromDate":"2012-12-01","toDate":"2012-12-02","delta":1.0},
                       {"fromDate":"2012-12-02","toDate":"2012-12-03","delta":1.0},
                       .
                       .
                       .]}
   },
   .
   .
   .
  }
  </code></pre>
  
  <p>Ideally I'd like to be able to return this data on an adhoc basis. I'm pretty sure this could be done using map-reduce, but I was wondering whether there was a way to do this using the new aggregation framework? Before I started researching mongodb I'd assumed modelling change in this way would be a faily common use case but so far I've not come across anything quite like it.</p>
  
  <p>Any advice you can give would be extremely useful. I'll update the post as my own research progresses.</p>
  
  <p>Thanks,</p>
  
  <p>Matt.</p>