title: $out stage of mongo aggregation pipeline not taking effect using node
body: <p>Long time listener, first time caller here.</p>
  
  <p>I'm using node drivers to execute an aggregate command on mongo and the <code>$out</code> stage seems to only take effect if certain methods are chained.</p>
  
  <p>I want to call <code>aggregate()</code> with $out as the last stage of the pipeline and then callback. Currently, it only works if I chain <code>next()</code> or <code>toArray()</code> which is irksome since the <a href="http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#aggregate" rel="nofollow noreferrer">method signature</a> is aggregate(pipeline, options, <strong>callback</strong>). </p>
  
  <p>Examples below simplified for clarity.</p>
  
  <p>Here, I use the callback and $out does NOT take effect (i.e. newNames isn't created but the callback is executed). I'm refactoring stuff from a few months back and this was working previously (version 2.2.33 of the node driver):</p>
  
  <pre><code>db.collection('names')
    .aggregate([
      { $match: {} }, 
      { $limit: 1 },
      { $out: 'newNames' }
    ], (err, result) =&gt; {
      // result is an AggregationCursor
      callback();
    });
  </code></pre>
  
  <p>If I don't add a callback but chain next() instead, the $out stage DOES take effect</p>
  
  <pre><code>db.collection('names')
    .aggregate([
      { $match: {} },
      { $limit: 1 },
      { $out: 'newNames' }
    ])
    .next((err, result) =&gt; {
      // result is null, why?
      callback();
    });
  </code></pre>
  
  <p>It also DOES work if you chain toArray:</p>
  
  <pre><code>db.collection('names')
    .aggregate([
      { $match: {} }, 
      { $limit: 1 },
      { $out: 'newNames' }
    ])
    .toArray((err, result) =&gt; {
      // result is an empty array (i.e. the resulting docs), OK, makes sense
      callback();
    });
  </code></pre>
  
  <p>So I thought I must be misunderstanding Promises vs callbacks, but it does NOT take effect if I use close() either which is chained and the result is back to being an AggregationCursor:</p>
  
  <pre><code>db.collection('names')
    .aggregate([
      { $match: {} },
      { $limit: 1 },
      { $out: 'newNames' }
    ])
    .close((err, result) =&gt; {
      // result is an AggregationCursor
      callback();
    });
  </code></pre>
  
  <p>Reading through some of the <a href="https://github.com/mongodb/node-mongodb-native/commit/9d12ac3cc64fed4bff75f7cc9667ab2f5c2cf305" rel="nofollow noreferrer">responses</a> to issues, it seems the AggregationCursor vs Resulting Documents is expected. But I don't get why $out isn't taking effect when I go to the callback or chain close().</p>
  
  <ul>
  <li>mongo 3.6.2 inside the mongo:latest docker image</li>
  <li>Docker 17.12.0</li>
  <li>node 8.9.0</li>
  </ul>