title: Use MongoDB array as stack
body: <p>Has anybody used MongoDB's Array type to implement a stack?</p>
  
  <p>I know that I can <code>append</code> to an Array like so:</p>
  
  <pre><code>db.blogposts.update( {_id:5}, {$push: {comments: {by: "Abe", text:"First"}}})
  </code></pre>
  
  <p>Here, the end of the array is the top of the stack... I don't see a way to implement this with the top of the stack at the zero'th index, but I'd love to be wrong.</p>
  
  <p>And I know that I can <code>peek</code> at the last value of the the array like so:</p>
  
  <pre><code>db.blogposts.find( {_id:5}, {comments: {$slice:-1}})
  </code></pre>
  
  <p>With an implementation like this, can I "peek" at the top of the stack in a MongoDB update statement?  That would give me the semantic, "push this item on the stack if the top of the stack is X".  I need this to be an atomic operation!</p>
  
  <p>Any advice appreciated. Thanks!</p>