title: Sorting on Multiple fields mongo DB
body: <p>I have a query in mongo such that I want to give preference to the first field and then the second field.</p>
  
  <p>Say I have to query such that</p>
  
  <pre><code>db.col.find({category: A}).sort({updated: -1, rating: -1}).limit(10).explain()
  </code></pre>
  
  <p>So I created the following index</p>
  
  <pre><code>db.col.ensureIndex({category: 1, rating: -1, updated: -1})
  </code></pre>
  
  <p>It worked just fined scanning as many objects as needed, i.e. <code>10</code>.</p>
  
  <p>But now I need to query </p>
  
  <pre><code>db.col.find({category: { $ne: A}}).sort({updated: -1, rating: -1}).limit(10)
  </code></pre>
  
  <p>So I created the following index:</p>
  
  <pre><code> db.col.ensureIndex({rating: -1, updated: -1})
  </code></pre>
  
  <p>but this leads to scanning of the whole document and when I create </p>
  
  <pre><code> db.col.ensureIndex({ updated: -1 ,rating: -1})
  </code></pre>
  
  <p>It scans less number of documents:</p>
  
  <p>I just want to ask to be clear about sorting on multiple fields and what is the order to be preserved when doing so. By reading the MongoDB documents, it's clear that the field on which we need to perform sorting should be the last field. So that is the case I assumed in my <code>$ne</code> query above. Am I doing anything wrong?</p>