title: sorting alpha with mongoose
body: <p>I'm trying to sort via mongoose 3.6.20 and I am receiving some unexpected results.</p>
  
  <p>I have a list of companies with a name. At first I thought that maybe it was sorting in a case sensitive way. Which based on articles, I expect was true.</p>
  
  <p>I'm now using a virtual property to down case the sort field. However, I'm still getting unexpected results.</p>
  
  <pre><code>CompanySchema.virtual('name_lower').get(function(){
    return this.name.toLowerCase();
  });
  </code></pre>
  
  <p>and when I sort</p>
  
  <pre><code>Company.find().sort({ name_lower: 1 });
  </code></pre>
  
  <p>I'm getting it in the following order:</p>
  
  <ol>
  <li>company name</li>
  <li>google</li>
  <li>company name (yes a duplicate for testing)</li>
  </ol>
  
  <p>I'm also outputting the value of my virtual property and it looks right. There is no whitespace or funky characters that would result in the 2nd 'company name' from appearing after google. </p>
  
  <p>Using nodejs, express, mongoose. </p>
  
  <p>What am I missing or doing incorrectly?</p>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>Based on the information provided in the answers, I refactored my schema to include some normalized fields and hooked into the pre save event of my document, where I update those normalized fields and sort using them in all future queries.</p>
  
  <pre><code>CompanySchema.pre('save', function(next){
    this.normalized_name = this.name;
  });
  </code></pre>
  
  <p>Next, is in the schema I use:</p>
  
  <pre><code>var CompanySchema = mongoose.Schema({
    ...
    normalized_name: { type: String, set: normalize },
    ...
  });
  </code></pre>
  
  <p>Where <code>normalize</code> is a function that for now, returns a lowercase version of the value passed into it. However, this allows me to expand on it later really fast, and I can quickly do the same to other fields that I might need to sort against.</p>