title: How to get the latest and oldest record in mongoose.js (or just the timespan between them)
body: <h2>Basic problem</h2>
  
  <p>I have a bunch of records and I need to get latest (most recent) and the oldest (least recent).</p>
  
  <p>When googling I found <a href="https://groups.google.com/forum/?fromgroups=#!searchin/mongoose-orm/oldest/mongoose-orm/z93TqQ2lHJ0/PdBpnZ-HPVEJ" rel="noreferrer">this topic</a> where I saw a couple of queries:</p>
  
  <pre><code>// option 1
  Tweet.findOne({}, [], { $orderby : { 'created_at' : -1 } }, function(err, post) {
    console.log( post );
  });
  // option 2
  Tweet.find({}, [], {sort:[['arrival',-1]]}, function(err, post) {
    console.log( post );
  });
  </code></pre>
  
  <p>Unfortunatly they both error:</p>
  
  <pre><code>TypeError: Invalid select() argument. Must be a string or object.
  </code></pre>
  
  <p>The link also has this one:</p>
  
  <pre><code>Tweet.find().sort('_id','descending').limit(15).find(function(err, post) {
    console.log( post );
  });
  </code></pre>
  
  <p>and that one errors:</p>
  
  <pre><code>TypeError: Invalid sort() argument. Must be a string or object.
  </code></pre>
  
  <p>So how can I get those records?</p>
  
  <h2>Timespan</h2>
  
  <p>Even more ideally I just want the difference in time (seconds?) between the oldest and the newest record, but I have no clue on how to start making a query like that.</p>
  
  <p>This is the schema:</p>
  
  <pre><code>var Tweet = new Schema({
      body: String
    , fid: { type: String, index: { unique: true } }
    , username: { type: String, index: true }
    , userid: Number
    , created_at: Date
    , source: String
  });
  </code></pre>
  
  <p>I'm pretty sure I have the most recent version of mongoDB and mongoose.</p>
  
  <h2>EDIT</h2>
  
  <p>This is how I calc the timespan based on the answer provided by JohnnyHK:</p>
  
  <pre><code>var calcDays = function( cb ) {
    var getOldest = function( cb ) {
      Tweet.findOne({}, {}, { sort: { 'created_at' : 1 } }, function(err, post) {
        cb( null, post.created_at.getTime() );
      });
    }
      , getNewest = function( cb ) {
      Tweet.findOne({}, {}, { sort: { 'created_at' : -1 } }, function(err, post) {
        cb( null, post.created_at.getTime() );
      });
    }
  
    async.parallel({ 
      oldest: getOldest
    , newest: getNewest
    }
      , function( err, results ) {
        var days = ( results.newest - results.oldest ) / 1000 / 60 / 60 / 24;
        // days = Math.round( days );
        cb( null, days );
      }
    );
  }
  </code></pre>