title: Insert document in mongodb with autoincrement field from java
body: <p>I am trying to insert a document with a sequence number, in one transaction, from java.</p>
  
  <p>Something similar to this:</p>
  
  <pre><code>function getNextSequence(name) {
      var ret = db.counters.findAndModify(
          {
              query: { _id: name },
              update: { $inc: { seq: 1 } },
              new: true
          }
      );
  
      return ret.seq;
  }
  collection.insert({
      number: getNextSequence("userid"),
      name: "Some Name"
  });
  </code></pre>
  
  <p>Is it possible to do this from java? Preferably with the official java driver.</p>