title: Mongoose indexing in production code
body: <p>Per the Mongoose <a href="http://mongoosejs.com/docs/guide.html" rel="noreferrer">documentation</a> for <code>MongooseJS</code> and <code>MongoDB</code>/<code>Node.js</code> : </p>
  
  <blockquote>
    <p>When your application starts up, Mongoose automatically calls <code>ensureIndex</code> for each defined index in your schema. While nice for development, it is recommended this behavior be disabled in production since index creation can cause a significant performance impact. Disable the behavior by setting the <code>autoIndex</code> option of your schema to false.</p>
  </blockquote>
  
  <p>This appears to instruct removal of auto-indexing from mongoose prior to deploying to optimize Mongoose from instructing Mongo to go and churn through all indexes on application startup, which seems to make sense.</p>
  
  <p>What is the proper way to handle indexing in production code?  Maybe an external script should generate indexes?  Or maybe <code>ensureIndex</code> is unnecessary if a single application is the sole reader/writer to a collection because it will continue an index every time a DB write occurs?</p>
  
  <p><strong>Edit:</strong> To supplement, MongoDB provides good <a href="http://docs.mongodb.org/manual/core/indexes/#index-creation-options" rel="noreferrer">documentation</a> for the <em>how</em> to do indexing, but not <em>why</em> or <em>when</em> explicit indexing directives should be done.  It seems to me that indexes should be kept up to date by writer applications automatically on collections with existing indexes and that <code>ensureIndex</code> is really more of a one-time thing (done when a new index is being applied), in which case Mongoose's <code>autoIndex</code> should be a no-op under a normal server restart.</p>