title: Auto increment a number field in mongoose model
body: <p>I was using MySql DB for my website and now updating it to MongoDB. There were columns in some tables getting auto-incremented and I want to make sure that the same continues. 
  Below are my finding so far but none is working.
  Using <strong>mongoose-auto-increment</strong> plugin</p>
  
  <pre><code>var mongoose = require('mongoose'),
      Schema = mongoose.Schema,
      autoIncrement = require('mongoose-auto-increment');
  
  var connection = mongoose.createConnection("mongodb://localhost/myDatabase");
  
  autoIncrement.initialize(connection);
  
  EmborderSchema.plugin(autoIncrement.plugin, {
    model: 'Emborder',
    field: 'emborder_id',
    startAt: 28855,
    increamentBy: 1
  });
  </code></pre>
  
  <p>Using pre save hook:</p>
  
  <pre><code>EmborderSchema.pre('save', function (next) {
    if (this.emborder_id == undefined) {
      Emborder.findOne({})
        .sort({emborder_id: -1})
        .exec(function (err, data) {
          this.emborder_id = data.emborder_id + 1;
          next();
        })
    } else {
      next();
    }
  });
  </code></pre>
  
  <p>Using the first method gives me an error </p>
  
  <blockquote>
    <p>E11000 duplicate key error collection: 24hd.24hd_emborder index: emborder_id_1 dup key: { 23345 }</p>
  </blockquote>
  
  <p>Second method gives this error</p>
  
  <blockquote>
    <p>E11000 duplicate key error collection: 24hd.24hd_emborder index: emborder_id_1 dup key: { : null }</p>
  </blockquote>
  
  <p>Note: emborder_id was the primary key in my old Mysql DB and in this code value of emborder_id is undefined as I am adding a new record.</p>
  
  <p>Please help me finding whats going wrong in this approch.</p>