title: how can i avoid mongodb race condition
body: <p>i want to update some data in mongodb,my logic is as follows:</p>
  
  <pre><code>#find the specific document with "md5,time,size",
  if collection.find({"src_md5":file_md5,"src_time":file_time,"src_size":file_size}).count() == 0:
      #if not found
      #   find the idx,if idx is not yet exist,set idx equa 1
  
      if collection.find({},{"idx":1}).count() == 0:
          idx = 1
  
      #if idx is alread there, sort idx and get the biggest idx
      else:
          idx = collection.find({},{"idx":1}).sort('idx',-1).limit(5)[0]['idx']
          idx = idx + 1
  
          #insert the info with idx
          if not self.insertFileInfo(collection,file_obj,file_md5,file_time,file_size,long(idx)):
              return None
  #if the specific document with "md5,time,size" is found
  else:
  #just get the idx with the specific md5
      idx = collection.find({"src_md5":file_md5,"src_time":file_time,"src_size":file_size},{"idx":1})[0]['idx']
      return None
  </code></pre>
  
  <p>i will run the above code in 4 machines,which means 4 process would update mongodb almost simultaneously,how can i ensure the atomic of the operations?
  my recored schema is</p>
  
  <pre><code>{"src_md5":"djapijfdakfiwqjfkasdj","src_size":2376498,"src_time":1338179291,"idx":1}
  {"src_md5":"jdfipajkoijjipjefjidwpj","src_size":234876323,"src_time":1338123873,"idx":2}
  {"src_md5":"djapojfkdasxkjipkjkf","src_size":3829874,"src_time":1338127634,"idx":3}
  </code></pre>
  
  <p>it's not a simple auto increment key,it should be increased when md5,size,time changed,and shuld be insert with them,as a record.
  i create a compound unique index on {"src_md5","src_time","src_size"},and create a unique index on {"idx"},but before i insert new info, i should get the idx alread exist,then increase it.
  there are two situation:
  1,idx with the specific md5,size,time,if is already exist,just return the idx
  2,if not  exist, increase idx with 1</p>