title: Mongodb: keeping a frequently written collection in RAM
body: <p>I am collecting data from a streaming API and I want to create a real-time analytics dashboard. This dashboard will display a simple timeseries plotting the number of documents per hour. I am wondering if my current approach is optimal.</p>
  
  <p>In the following example, on_data is fired for each new document in the stream.</p>
  
  <pre><code># Mongo collections.
  records = db.records
  stats = db.records.statistics
  
  on_data(self, data):
      # Create a json document from data.
      document = simplejson.loads(data)
  
      # Insert the new document into records.
      records.insert(document)
  
      # Update a counter in records.statistics for the hour this document belongs to. 
      stats.update({ 'hour': document['hour'] }, { '$inc': { document['hour']: 1 } }, upsert=True)
  </code></pre>
  
  <p>The above works. I get a beautiful graph which plots the number of documents per hour. My question is about whether this approach is optimal or not. I am making two Mongo requests per document. The first inserts the document, the second updates a counter. The stream sends approximately 10 new documents a second. </p>
  
  <p>Is there for example anyway to tell Mongo to keep the db.records.statistics in RAM? I imagine this would greatly reduce disk access on my server.</p>