title: Auto increment sequence in mongodb using java
body: <p>Hi I want to auto increment <code>_id</code> in mongodb using java. I am completely new to this. In the document I found the solution like this: </p>
  
  <pre><code>db.counters.insert(
   {
        _id: "userid",
        seq: 0
     }
  )
  
  
  function getNextSequence(name) {
  var ret = db.counters.findAndModify(
  {
  query: { _id: name },
  update: { $inc: { seq: 1 } },
  new: true
  }
  );
  return ret.seq;
  }
  
  db.users.insert(
  {
  _id: getNextSequence("userid"),
  name: "Sarah C."
  }
  )
  </code></pre>
  
  <p>Can any one suggest how do I do this using java ? I am completely new to this.</p>