function _next(ctx, cb) {
  var callback = cb;
  if (ctx._transforms.length) {
    callback = function(err, doc) {
      if (err || doc === null) {
        return cb(err, doc);
      }
      cb(err, ctx._transforms.reduce(function(doc, fn) {
        return fn(doc);
      }, doc));
    };
  }

  if (ctx._error) {
    return process.nextTick(function() {
      callback(ctx._error);
    });
  }

  if (ctx.cursor) {
    return ctx.cursor.next(function(error, doc) {
      if (error) {
        return callback(error);
      }
      if (!doc) {
        return callback(null, null);
      }

      var opts = ctx.query._mongooseOptions;
      if (!opts.populate) {
        return opts.lean === true ?
                callback(null, doc) :
                _create(ctx, doc, null, callback);
      }

      var pop = helpers.preparePopulationOptionsMQ(ctx.query,
        ctx.query._mongooseOptions);
      pop.forEach(function(option) {
        delete option.model;
      });
      pop.__noPromise = true;
      ctx.query.model.populate(doc, pop, function(err, doc) {
        if (err) {
          return callback(err);
        }
        return opts.lean === true ?
          callback(null, doc) :
          _create(ctx, doc, pop, callback);
      });
    });
  } else {
    ctx.once('cursor', function() {
      _next(ctx, cb);
    });
  }
}
/*!
 * Get the next doc from the underlying cursor and mongooseify it
 * (populate, etc.)
 */
Get the next doc from the underlying cursor and mongooseify it
(populate, etc.)
Get the next doc from the underlying cursor and mongooseify it ( populate etc . )
function _next(ctx, cb) {
  var callback = cb;
  if (ctx._transforms.length) {
    callback = function(err, doc) {
      if (err || doc === null) {
        return cb(err, doc);
      }
      cb(err, ctx._transforms.reduce(function(doc, fn) {
        return fn(doc);
      }, doc));
    };
  }
  if (ctx._error) {
    return process.nextTick(function() {
      callback(ctx._error);
    });
  }
  if (ctx.cursor) {
    return ctx.cursor.next(function(error, doc) {
      if (error) {
        return callback(error);
      }
      if (!doc) {
        return callback(null, null);
      }
      var opts = ctx.query._mongooseOptions;
      if (!opts.populate) {
        return opts.lean === true ?
                callback(null, doc) :
                _create(ctx, doc, null, callback);
      }
      var pop = helpers.preparePopulationOptionsMQ(ctx.query,
        ctx.query._mongooseOptions);
      pop.forEach(function(option) {
        delete option.model;
      });
      pop.__noPromise = true;
      ctx.query.model.populate(doc, pop, function(err, doc) {
        if (err) {
          return callback(err);
        }
        return opts.lean === true ?
          callback(null, doc) :
          _create(ctx, doc, pop, callback);
      });
    });
  } else {
    ctx.once('cursor', function() {
      _next(ctx, cb);
    });
  }
}
function _next ( ctx , cb ) { var callback = cb ; if ( ctx . _transforms . length ) { callback = function ( err , doc ) { if ( err || doc === null ) { return cb ( err , doc ) ; } cb ( err , ctx . _transforms . reduce ( function ( doc , fn ) { return fn ( doc ) ; } , doc ) ) ; } ; } if ( ctx . _error ) { return process . nextTick ( function ( ) { callback ( ctx . _error ) ; } ) ; } if ( ctx . cursor ) { return ctx . cursor . next ( function ( error , doc ) { if ( error ) { return callback ( error ) ; } if ( ! doc ) { return callback ( null , null ) ; } var opts = ctx . query . _mongooseOptions ; if ( ! opts . populate ) { return opts . lean === true ? callback ( null , doc ) : _create ( ctx , doc , null , callback ) ; } var pop = helpers . preparePopulationOptionsMQ ( ctx . query , ctx . query . _mongooseOptions ) ; pop . forEach ( function ( option ) { delete option . model ; } ) ; pop . __noPromise = true ; ctx . query . model . populate ( doc , pop , function ( err , doc ) { if ( err ) { return callback ( err ) ; } return opts . lean === true ? callback ( null , doc ) : _create ( ctx , doc , pop , callback ) ; } ) ; } ) ; } else { ctx . once ( 'cursor' , function ( ) { _next ( ctx , cb ) ; } ) ; } }
Get the next doc from the underlying cursor and mongooseify it
 (populate, etc.)
Get the next doc from the underlying cursor and mongooseify it ( populate etc . )