title: Limit inserts in mongodb
body: <p>I enter these documents in a table:</p>
  
  <pre><code>db.RoomUsers.insert( {userId: 1, roomId: 1} )
  db.RoomUsers.insert( {userId: 2, roomId: 1} )
  db.RoomUsers.insert( {userId: 3, roomId: 1} )
  db.RoomUsers.insert( {userId: 4, roomId: 1} )
  </code></pre>
  
  <p>Now, my application requires that in RoomUsers there can be a limited number of user in each room. Let's say that there cannot be more than 5 user for each room.
  How to fix that?</p>
  
  <p>If I was using an RDBM I could maybe have this strategy(Im not sure its the best one, but still):</p>
  
  <pre><code>1 - Count number of entries in RoomUsers where roomId = X
  2 - If number of users is less than Y then:
  2A - Start a transaction
  2B - Insert new user in RoomUsers
  2C - Count number of entries in RoomUsers where roomId = X
  2D - If number of users is greater than Y then: Rollback. Otherwise: commit
  </code></pre>
  
  <p>MongoDB doesn't really have transaction, what I understand. How to accomplish the same thing in noSql?</p>