title: Auto incrementing field in mongoose
body: <p>I'm trying to implement an auto incrementing field in mongodb as highlighted in the docs (<a href="http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/" rel="nofollow">http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/</a>). However, I'm not exactly sure what is the best way to do so in mongoose.</p>
  
  <p>I have a CounterSchema and a UserSchema, and I'm wondering where to put the getNextSequence function, and how to call it from the User Schema? </p>
  
  <pre><code>//counter.js
  var CounterSchema = new Schema({
    category: String,
    seq: Number
  });
  
  //done in mongo shell
  db.counters.insert({category: 'userIndex', seq: 0})
  
  //user.js
  var UserSchema = new Schema({
    name: String,
    UserIndex: Number
  }
  
  //per mongodb docs -&gt; not sure where I should insert it
  function getNextSequence(name) {
     var ret = db.counters.findAndModify(
            {
              query: { _id: name },
              update: { $inc: { seq: 1 } },
              new: true
            }
     );
  
     return ret.seq;
  }
  </code></pre>