Here is a comparison between toArray() and cursors after find() in the MongoDB Node.js. driver Common code:
var MongoClient = require('mongodb').MongoClient, assert = require('assert'); MongoClient.connect('mongodb://localhost:27017/crunchbase', function (err, db) { assert.equal(err, null); console.log('Successfully connected to MongoDB.'); const query = { category_code: "biotech" };
Here is the code toArray() which goes in the section above.
db.collection('companies').find(query).toArray(function (err, docs) { assert.equal(err, null); assert.notEqual(docs.length, 0); docs.forEach(doc => { console.log('${doc.name} is a ${doc.category_code} company.'); }); db.close(); });
According to the documentation,
The caller is responsible for ensuring that there is enough memory to store the results.
Here's a cursor based approach using the cursor.forEach() method:
const cursor = db.collection('companies').find(query); cursor.forEach( function (doc) { console.log('${doc.name} is a ${doc.category_code} company.'); }, function (err) { assert.equal(err, null); return db.close(); } ); });
When using the forEach() method, instead of extracting all the data from memory, we pass the data to our application. find() immediately creates a cursor because it does not actually query the database until we try to use some of the documents that it will provide. cursor should describe our request. The second parameter to cursor.forEach shows what to do when an error occurs.
In the original version of the above code, it was toArray() called the database call. This means that we need ALL documents and we want them to be in an array .
Note that MongoDB returns data in batch mode. The figure below shows the requests from cursors (from the application) to MongoDB :

forEach scales better than toArray because we can process documents as they arrive until we reach the end. Compare this to toArray - where we expect to receive ALL documents and build the entire array. This means that we do not get any benefits from the fact that the driver and the database system work together to batch process the results for your application. Packaging is designed to be efficient in terms of memory overhead and runtime. Use this in your application if you can.