title: Mongoose Count documents
body: <p>I've got the below query to return a random user from my users schema:</p>
  
  <pre><code>UserSchema.statics.random = function(id,callback) {
    this.count(function(err, count) {
      if (err) {
        return callback(err);
      }
      var rand = Math.floor(Math.random() * count);
      this.findOne()
      .where('_id').ne(id)
      .where('last_active').gte(new Date(new Date().setDate(new Date().getDate()-3)))
      .skip(rand)
      .exec(callback);
    }.bind(this));
  };
  </code></pre>
  
  <p>However sometime it returns <code>NULL</code> - I'd think this is because it's counting the documents first then applying filters which reduces the count of documents so the <code>rand</code> value could be higher than the number of documents available.</p>
  
  <p>I can't seem to think what a better way would be to do this? </p>
  
  <p>Would I run the above query, count the documents then run another query with the <code>.skip()</code> parameter?</p>