Q:

mongo cursor timeout

I am trying to aggregate some records in a mongo database using the node driver. I am first matching to org, fed, and sl fields (these are indexed). If I only include a few companies in the array that I am matching the org field to, the query runs fine and works as expected. However, when including all of the clients in the array, I always get:
MongoError: getMore: cursor didn't exist on server, possible restart or timeout?

I have tried playing with the allowDiskUse, and the batchSize settings, but nothing seems to work. With all the client strings in the array, the aggregation runs for ~5hours before throwing the cursor error. Any ideas? Below is the pipeline along with the actual aggregate command.
setting up the aggregation pipeline:
var aggQuery = [

{
  $match: { //all clients, from last three days, and scored
    org: 
      { $in : array } //this is the array I am talking about
    ,
    frd: {
      $gte: _.last(util.lastXDates(3))
    },
    sl : true
  }
}

, {
  $group: { //group by isp and make fields for calculation
    _id: "$gog",
    count: {
      $sum: 1
    },
    countRisky: {
      $sum: {
        $cond: {
          if :{
            $gte: ["$scr", 65]
          },
          then: 1,
          else :0
        }
      }
    },
    countTimeZoneRisky: {
      $sum: {
        $cond: {
          if :{
            $eq: ["$gmt", "$gtz"]
          },
          then: 0,
          else :1
        }
      }
    }
  }
}

, {
  $match: { //show records with count >= 500
    count: {
      $gte: 500
    }
  }
}
, {
  $project: { //rename _id to isp, only show relevent fields
    _id: 0,
    ISP: "$_id",
    percentRisky: {
      $multiply: [{
          $divide: ["$countRisky", "$count"]
        },
        100
      ]
    },
    percentTimeZoneDiscrancy: {
      $multiply: [{
          $divide: ["$countTimeZoneRisky", "$count"]
        },
        100
      ]
    },
    count: 1
  }
}

, {
  $sort: { //sort by percent risky and then by count
    percentRisky: 1,
    count: 1
  }
}

];

Running the aggregation:
  var cursor = reportingCollections.hitColl.aggregate(aggQuery, {
    allowDiskUse: true,
    cursor: {
      batchSize: 40000
    }
  });

  console.log('Writing data to csv ' + currentFileNamePrefix + '!');

  //iterate through cursor and write documents to CSV
  cursor.each(function (err, document) {
    //write each document to csv file
    //maybe start a nuclear war
  });

A:

You're calling the aggregate method which doesn't return the cursor by default (like e.g. find()). To return query as a cursor, you must add the cursor option in the options. But, the timeout setting for the aggregation cursor is (currently) not supported. The native node.js driver only supports the batchSize setting. 
You would set the batchOption like this:
var cursor = coll.aggregate(query, {cursor: {batchSize:100}}, writeResultsToCsv);

