title: What is the best way to implement a FIFO collection in mongoDB?
body: <p>I have a sorted time series of items. Every 1 second I want to insert a document that contains an arbitrary count:</p>
  
  <p><code>{ count: Number }</code></p>
  
  <p>Then every 5 seconds I want to remove the first documents that were inserted until the cumulative sum of the count in those documents is greater than 10.</p>
  
  <p>So I have a list (collection) like this:</p>
  
  <p><code>[{ count: 5 }, { count: 3 }, { count: 2 }, { count: 7 }, { count: 8 }]</code></p>
  
  <p>Then after removing the first documents it becomes</p>
  
  <p><code>[{ count: 7 }, { count: 8 }]</code></p>
  
  <p>because <code>5 + 3 + 2 == 10</code> I stop removing documents.</p>
  
  <p>The list is guaranteed to be sorted so that only elements from the beginning of the list will be removed and elements will only be appended to the end.</p>
  
  <p>What is the best way to implement a FIFO list like this so that removing elements is O(1) and appending elements is also O(1)?</p>