title: Java/mongodb - How to implement aggregate method to return result as cursor
body: <p>I am facing below issue</p>
  
  <pre><code>com.mongodb.CommandFailureException: { "serverUsed" : "127.0.0.1:15847" , "errmsg" : "exception: aggregation result exceeds maximum document size (16MB)" , "code" : 16389 , "ok" : 0.0}
  </code></pre>
  
  <p>After googling I found some work around like using below code </p>
  
  <ol>
  <li><code>{"$out":"temp_colls");</code></li>
  <li><code>useCursor=True</code></li>
  </ol>
  
  <p>1st one is working for me but its taking too much time around 3-4 minutes.</p>
  
  <p>So looking for second option I have tried with below</p>
  
  <p>Original code is </p>
  
  <pre><code>dbObjArray = new BasicDBObject[2]
  dbObjArray[0]= cruxLevel
  dbObjArray[1] = project                                  
  List&lt;DBObject&gt; pipeline = Arrays.asList(dbObjArray)
         if (!datasetObject?.isFlat &amp;&amp; jsonFor != 'collection-grid') {
      output= dataSetCollection.aggregate(pipeline)
         }else{
              output= dataSetCollection.aggregate(project)
         }
  output.results().eachWithIndex{list,index-&gt;
  dataList.add(output.results()[index])
  </code></pre>
  
  <p>I have tried below to get result in <strong>cursor</strong>.</p>
  
  <p>1.</p>
  
  <pre><code>   // Assuming MongoCollection
     dataSetCollection.aggregate(pipeline).useCursor(true)
  </code></pre>
  
  <p>You might also need to tell it to use disk space on the server rather than doing it all in memory:</p>
  
  <p>2.</p>
  
  <pre><code>  // Assuming MongoCollection
    dataSetCollection.aggregate(pipeline).useCursor(true).allowDiskUse(true)
  </code></pre>
  
  <p>If you're using an older driver (or the old API in the new driver) those two options would look like this:</p>
  
  <p>3.</p>
  
  <pre><code>   // Assuming DBCollection
      dataSetCollection.aggregate(pipeline, AggregationOptions.builder().allowDiskUse(true)
      .useCursor(true)
      .build())
     .useCursor(true)
  </code></pre>
  
  <p>And </p>
  
  <pre><code> output= dataSetCollection.aggregate(project).newAggregationOptions().outputMode(AggregationOptions.OutputMode.CURSOR).build()
  </code></pre>
  
  <p>Giving error for example</p>
  
  <p>For  <em>"output= dataSetCollection.aggregate(project).useCursor(true)"</em> </p>
  
  <pre><code>groovy.lang.MissingMethodException: No signature of method: com.mongodb.AggregationOutput.useCursor() is applicable for argument types: (java.lang.Boolean) values: [true]
  </code></pre>
  
  <p>For <em>"output= dataSetCollection.aggregate(project).useCursor(true).allowDiskUse(true)"</em></p>
  
  <pre><code>groovy.lang.MissingMethodException: No signature of method: com.mongodb.AggregationOutput.useCursor() is applicable for argument types: (java.lang.Boolean) values: [true]
  </code></pre>
  
  <p>For <em>"output= dataSetCollection.aggregate(project,AggregationOptions.builder()
      .allowDiskUse(true)
          .useCursor(true)
          .build())
      .useCursor(true)"</em></p>
  
  <pre><code>groovy.lang.MissingPropertyException: No such property: AggregationOptions for class: com.acumetric.hrat.aggregator.DataImportService
  </code></pre>
  
  <p>And for last one respectively.</p>
  
  <pre><code>groovy.lang.MissingMethodException: No signature of method: com.mongodb.AggregationOutput.newAggregationOptions() is applicable for argument types: () values: []
  </code></pre>
  
  <p>Nothing is working. Where I am going wrong please help!</p>
  
  <p>Thank you.</p>