title: Mongo aggregation cursor & counting
body: <p>According to the <a href="http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#aggregate">mongodb node driver docs</a> the aggregate function now returns a cursor (from 2.6).</p>
  
  <p>I hoped that I could use this to get a count of items pre limit &amp; skipping but there doesn't seem to be any count function on the created cursor.  If I run the same queries in the mongo shell the cursor has an itcount function that I can call to get what I want.</p>
  
  <p>I saw that the created cursor has an on data event (does that mean it's a <a href="http://mongodb.github.io/node-mongodb-native/api-generated/cursorstream.html">CursorStream</a>?) which seemed to get triggered the expected number of times, but if I use it in combination with cursor.get no results get passed into the callback function.</p>
  
  <p>Can the new cursor feature be used to count an aggregation query?</p>
  
  <p>Edit for code:</p>
  
  <p>In mongo shell:</p>
  
  <pre><code>&gt; db.SentMessages.find({Type : 'Foo'})
  { "_id" : ObjectId("53ea19af9834184ad6d3675a"), "Name" : "123", "Type" : "Foo" }
  { "_id" : ObjectId("53ea19dd9834184ad6d3675c"), "Name" : "789", "Type" : "Foo" }
  { "_id" : ObjectId("53ea19d29834184ad6d3675b"), "Name" : "456", "Type" : "Foo" }
  
  &gt; db.SentMessages.find({Type : 'Foo'}).count()
  3
  
  &gt; db.SentMessages.find({Type : 'Foo'}).limit(1)
  { "_id" : ObjectId("53ea19af9834184ad6d3675a"), "Name" : "123", "Type" : "Foo" }
  
  &gt; db.SentMessages.find({Type : 'Foo'}).limit(1).count();
  3
  
  &gt; db.SentMessages.aggregate([ { $match : { Type : 'Foo'}} ])
  { "_id" : ObjectId("53ea19af9834184ad6d3675a"), "Name" : "123", "Type" : "Foo" }
  { "_id" : ObjectId("53ea19dd9834184ad6d3675c"), "Name" : "789", "Type" : "Foo" }
  { "_id" : ObjectId("53ea19d29834184ad6d3675b"), "Name" : "456", "Type" : "Foo" }
  
  &gt; db.SentMessages.aggregate([ { $match : { Type : 'Foo'}} ]).count()
  2014-08-12T14:47:12.488+0100 TypeError: Object #&lt;Object&gt; has no method 'count'
  
  &gt; db.SentMessages.aggregate([ { $match : { Type : 'Foo'}} ]).itcount()
  3
  
  &gt; db.SentMessages.aggregate([ { $match : { Type : 'Foo'}}, {$limit : 1} ])
  { "_id" : ObjectId("53ea19af9834184ad6d3675a"), "Name" : "123", "Type" : "Foo" }
  
  &gt; db.SentMessages.aggregate([ { $match : { Type : 'Foo'}}, {$limit : 1} ]).itcount()
  1
  
  &gt; exit
  bye
  </code></pre>
  
  <p>In Node:</p>
  
  <pre><code>var cursor = collection.aggregate([ { $match : { Type : 'Foo'}}, {$limit : 1} ], { cursor : {}});
  
  cursor.get(function(err, res){
    // res is as expected (1 doc)
  });
  </code></pre>
  
  <p>cursor.count() does not exist</p>
  
  <p>cursor.itcount() does not exist</p>
  
  <p>The on data event exists:</p>
  
  <pre><code>cursor.on('data', function(){
      totalItems++;
  });
  </code></pre>
  
  <p>but when used in combination with cursor.get, the .get callback function now contains 0 docs</p>
  
  <p>Edit 2:  The cursor returned appears to be an <a href="https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/aggregation_cursor.js">aggregation cursor</a> rather than one of the cursors listed in the docs</p>