title: Sorting in MongoDB's aggregation framework
body: <p><a href="http://docs.mongodb.org/manual/reference/aggregation/sort/" rel="nofollow">The docs</a> for MongoDB seem to suggest that in order to sort the results of an <code>aggregate</code> call you should specify a dictionary/object like this:</p>
  
  <pre><code>db.users.aggregate(
      { $sort : { age : -1, posts: 1 } }
  );
  </code></pre>
  
  <p>This is supposed to sort by <code>age</code> and then by <code>posts</code>.</p>
  
  <p>What do I do if I want to sort by <code>posts</code> and then by <code>age</code>? Changing the order of the keys seems to have no effect, probably because this is a JS object's properties. In other words, sorting, it seems, is always according to the lexical order of the keys, which seems rather odd as a design choice...</p>
  
  <p>Am I missing something? Is there a way to specify an <strong>ordered</strong> list of keys to sort by?</p>