title: How to perform a "decrement and delete when zero" with MongoDB
body: <p>I have a document type in MongoDB (ErrorTypeOccurences) that simply has a Name and Count property. I increment/decrement the Count property every time an exception is logged (to be able to report on application error types). For doing updates, I've discovered how to do an upsert with the $inc modifier, which is great, but I'm struggling to figure out a clean way to do a decrement operation that will also delete the document if the decremented value equals zero (since no error logs will exist of that type anymore).</p>
  
  <p>I can obviously do the decrement operation and then remove any error documents whose Count is zero but I'd love to do this in an atomic operation, completely on the server. findAndModifiy looked promising, but I don't see how I could do the decrement update and then conditionally remove the record if it's decremented count was now equal to zero.</p>
  
  <p>Could I achieve this with standard operations or do I need to write a server-side function?</p>
  
  <p>I'm using C#/NoRM, so examples using that would be awesome but not at all required. Thanks.</p>