title: MongoDB, array field of objects with incremental property
body: <p>I understand the question is a bit confusing. Bear with me,</p>
  
  <p>Let's say I have a Class document which contains Students property as array. Now these students should have an incremental Id but I am not sure how to do that when there are concurrent <s>inserts</s> push queries to the document.</p>
  
  <p>Example:</p>
  
  <p>let's say class is my document in db.</p>
  
  <pre><code>var class = {
     'students':[{
        'name': 'Bob'
         'id' :1
     }]
  }
  </code></pre>
  
  <p>now if I am to <s>insert</s> push the student Sally (<code>{'name': 'Sally'}</code>) to <code>class.students</code> I would like her to get 2 as id.</p>
  
  <p>I could query to get the latest id in the db and increment that by one and use it as Sally's id. </p>
  
  <p>However, I don't know what happens when two different requests were made concurrently and one of them tries to <s>insert</s> push Sally and the other Jack. How can I make sure Sally and Jack doesn't get the same id?</p>
  
  <p>A note; I don't want to rely on the array index for id field.</p>