title: Why has mongo ascending/descending sorting order flipped for a Date field?
body: <p>Mongo version: 2.6.0</p>
  
  <p>I am not sure I understand this. It looks like mongo DB sort order has flipped for one of my fields somehow?</p>
  
  <p>Here is ASCENDING order sort:</p>
  
  
  
  <pre class="lang-js prettyprint-override"><code>&gt; db.clicks.find({}, {"time": 1}).sort({"time": 1}).limit(1).pretty()
  {
          "_id" : ObjectId("5367e87fb820307109000cdc"),
          "time" : ISODate("2014-05-05T19:37:25Z")
  }
  </code></pre>
  
  <p>and DESCENDING order sort:</p>
  
  <pre class="lang-js prettyprint-override"><code>&gt; db.clicks.find({}, {"time": 1}).sort({"time": -1}).limit(1).pretty()
  {
          "_id" : ObjectId("53607a5bb820301547000242"),
          "time" : ISODate("2014-04-30T04:21:31Z")
  }
  </code></pre>
  
  <p>This flipping is happening only for <code>time</code> field.</p>
  
  <p>If I sort on another field, it works fine:</p>
  
  <pre class="lang-js prettyprint-override"><code>&gt; db.clicks.find({}, {"lid": 1}).sort({"lid": 1}).limit(1).pretty()
  { "_id" : ObjectId("5363f859b820306ca20002aa"), "lid" : 55960 }
  &gt; db.clicks.find({}, {"lid": 1}).sort({"lid": -1}).limit(1).pretty()
  { "_id" : ObjectId("5364488bb8203071090002db"), "lid" : 2074671 }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>UPDATE: Looks like something strange is going on, since fetching more results is giving a totally random order.</p>
  
  <pre class="lang-js prettyprint-override"><code>&gt; db.clicks.find({}, {"time": 1}).sort({"time": 1}).limit(5).pretty()
  {
          "_id" : ObjectId("5367e87fb820307109000cdc"),
          "time" : ISODate("2014-05-05T19:37:25Z")
  }
  {
          "_id" : ObjectId("53618d7eb820306ca200002e"),
          "time" : ISODate("2014-04-30T23:55:30Z")
  }
  {
          "_id" : ObjectId("53675870b820307109000876"),
          "time" : ISODate("2014-05-05T09:22:48Z")
  }
  {
          "_id" : ObjectId("53642974b820307109000072"),
          "time" : ISODate("2014-05-02T23:25:20Z")
  }
  {
          "_id" : ObjectId("535e6a11b8203015470000e5"),
          "time" : ISODate("2014-04-28T14:47:25Z")
  }
  </code></pre>
  
  <p><code>time</code> is an indexed field (and used for TTL):</p>
  
  <pre class="lang-js prettyprint-override"><code>&gt; db.clicks.getIndexes()
  [
      ...
          {
                  "v" : 1,
                  "key" : {
                          "time" : 1
                  },
                  "name" : "time_1",
                  "ns" : "raw.clicks",
                  "expireAfterSeconds" : 7776000
          }
  ]
  </code></pre>
  
  <p>UPDATE 2: I deleted the index and rebuilt it. </p>
  
  <pre class="lang-js prettyprint-override"><code>&gt; db.clicks.dropIndex({"time": 1})
  { "nIndexesWas" : 10, "ok" : 1 }
  &gt; db.clicks.ensureIndex({"time": 1}, {"expireAfterSeconds" : 7776000})
  {
          "createdCollectionAutomatically" : false,
          "numIndexesBefore" : 9,
          "numIndexesAfter" : 10,
          "ok" : 1
  }
  </code></pre>
  
  <p>Still the random ordering was happening.</p>
  
  <p>Then I deleted all the documents from the collection:</p>
  
  <pre class="lang-js prettyprint-override"><code>&gt; db.clicks.remove({})
  WriteResult({ "nRemoved" : 130 })
  </code></pre>
  
  <p>and added new documents and reran the query:</p>
  
  <pre class="lang-js prettyprint-override"><code>&gt; db.clicks.find({}, {"time": 1}).sort({"time": 1}).limit(5).pretty()
  {
          "_id" : ObjectId("5367f9a2b820307109000f2e"),
          "time" : ISODate("2014-05-05T20:50:36Z")
  }
  {
          "_id" : ObjectId("5367f9ccb820307109000f6f"),
          "time" : ISODate("2014-05-05T20:51:04Z")
  }
  {
          "_id" : ObjectId("5367f9b7b820307109000f3d"),
          "time" : ISODate("2014-05-05T20:50:54Z")
  }
  {
          "_id" : ObjectId("5367f9b7b820307109000f3e"),
          "time" : ISODate("2014-05-05T20:50:58Z")
  }
  {
          "_id" : ObjectId("5367f9b7b820307109000f3f"),
          "time" : ISODate("2014-05-05T20:51:00Z")
  }
  </code></pre>
  
  <p>Still having the same problem. Is it because the index on time field is a TTL index? </p>
  
  <p>UPDATE 3: Ok, so there is something wrong on this specific mongo server which I cannot quite figure out. I ran the queries on another mongo server and the queries are working fine (collection and indexes are the same): </p>
  
  <pre class="lang-js prettyprint-override"><code>&gt; db.clicks.find({}, {"_id": 0, "time": 1}).sort({"time": 1}).limit(10).pretty()
  { "time" : ISODate("2014-04-25T23:04:57Z") }
  { "time" : ISODate("2014-04-25T23:04:57Z") }
  { "time" : ISODate("2014-04-25T23:04:58Z") }
  { "time" : ISODate("2014-04-25T23:04:59Z") }
  { "time" : ISODate("2014-04-25T23:04:59Z") }
  { "time" : ISODate("2014-04-25T23:04:59Z") }
  { "time" : ISODate("2014-04-25T23:05:03Z") }
  { "time" : ISODate("2014-04-25T23:05:09Z") }
  { "time" : ISODate("2014-04-25T23:05:10Z") }
  { "time" : ISODate("2014-04-25T23:05:10Z") }
  
  &gt; db.clicks.find({}, {"_id": 0, "time": 1}).sort({"time": -1}).limit(10).pretty()
  { "time" : ISODate("2014-05-05T22:30:22Z") }
  { "time" : ISODate("2014-05-05T22:30:19Z") }
  { "time" : ISODate("2014-05-05T22:30:19Z") }
  { "time" : ISODate("2014-05-05T22:30:18Z") }
  { "time" : ISODate("2014-05-05T22:30:16Z") }
  { "time" : ISODate("2014-05-05T22:30:16Z") }
  { "time" : ISODate("2014-05-05T22:30:16Z") }
  { "time" : ISODate("2014-05-05T22:30:15Z") }
  { "time" : ISODate("2014-05-05T22:30:14Z") }
  { "time" : ISODate("2014-05-05T22:30:13Z") }
  </code></pre>