title: mongodb java driver converting large result set to json
body: <p>I am running a query on ~120 million records in mongodb. The query executes quickly both through the mongo shell and using the Java drivers, however when I try to convert the result to a json object through the Java drivers, it is very slow (query takes &lt; 100ms but the converting to json takes > 30s). The result set has about 5k items. I'm doing the conversion with <code>JSON.serialize(cursor)</code>.</p>
  
  <p>While I expect it to take a little bit of time to convert to a json string, if I run the query from the shell but doing</p>
  
  <pre><code>var cursor = //execute query
  var arr = cursor.toArray();
  arr
  </code></pre>
  
  <p>It prints out very quickly.</p>
  
  <p>The mongo server stats are reporting an increasing number of page faults during the serializing process, but I have increased my RAM to be much larger than the entire collection plus indexes.</p>
  
  <p>Any thoughts on what might be happening here and how to improve the speed of the conversion to json?</p>