title: MongoDB: How to get the newest documents within a collection by using _id?
body: <p>How would I retrieve the newest documents created in my collection by using _id? </p>
  
  <p>I'm trying to implement a pagination in my web application by:</p>
  
  <ol>
  <li>Retrieving the _id of the last document loaded in my page</li>
  <li>Retrieve documents greater than “_id” of the last document in the next page
  (<a href="http://tldrify.com/dcj" rel="nofollow">http://tldrify.com/dcj</a>)</li>
  </ol>
  
  <p>The reason I took this approach was because I was trying to leverage the natural order in the stored data - the id stored in the document. </p>
  
  <p>Example of getting next set of posts: </p>
  
  <pre><code>Posts.find({ _id : { $gt : lastObjectId } }, function(){
    if(error){
      console.log('error in get_next_posts');
    } else {
      response.json(results);
    }
  }).limit(5)
  </code></pre>
  
  <p>My mongoose schema:</p>
  
  <pre><code>var PostSchema = new mongoose.Schema({
    upvotes: {type: Number, default: 0},
    comments: Array,
    imageURI: String,
    caption: String,
    location: Object,
    hashtag: String
  });
  </code></pre>
  
  <p>To my understanding, Mongo's documents are already sorted from oldest to newest, as per the documentation :</p>
  
  <p>"MongoDB creates the _id index, which is an ascending unique index on the _id field, for all collections when the collection is created.
  (<a href="https://docs.mongodb.org/v3.0/core/index-single/" rel="nofollow">https://docs.mongodb.org/v3.0/core/index-single/</a>)"</p>
  
  <p>All was working well initially after pushing a few posts to my mongoDB database, which is hosted on Heroku (seems to be posting in ascending order)</p>
  
  <pre><code>Post1 id --- 56756e2047b977030017b36d
  Post2 id --- 56756e4f47b977030017b36e
  Post3 id --- 56756e8447b977030017b36f
  Post4 id --- 56756ef747b977030017b370
  Post5 id --- 56756f3f47b977030017b371
  Post6 id --- 56756f7247b977030017b372
  </code></pre>
  
  <p>But after adding my 7th and 8th posts to the collection, it seems my Mongo database has suddenly stopped sorting the posts in ascending order, which means I can no longer use my previous mongoose query.</p>
  
  <p>I'm not sure why after a few posts to my database the ascending order just stops - anyone have an idea why?</p>
  
  <pre><code>Post1 id --- 56756e2047b977030017b36d
  Post7 id --- 5675727647b977030017b374   &lt;-- why are they here now?
  Post8 id --- 5675744847b977030017b375   &lt;--
  Post2 id --- 56756e4f47b977030017b36e
  Post3 id --- 56756e8447b977030017b36f
  Post4 id --- 56756ef747b977030017b370
  Post5 id --- 56756f3f47b977030017b371
  Post6 id --- 56756f7247b977030017b372
  </code></pre>
  
  <p>UPDATE: this is how I ended up getting oldest posts by _id:</p>
  
  <pre><code>Post.find({}, function(error, results) {
    if(error) {
      console.log('error in show');
    } else {
      console.log('show query successful');
      response.json(results);
    }
  }).sort({_id:1})
  </code></pre>