Search code examples
node.jsnode-mongodb-native

What does err and doc mean in cursor.toArray(function(err, docs){})?


This is from official MongoDB documentation.

toArray cursor.toArray(function(err, docs){}) converts the cursor object into an array of all the matching records. Probably the most convenient way to retrieve results but be careful with large datasets as every record is loaded into memory.

collection.find().toArray(function(err, docs){
    console.log("retrieved records:");
    console.log(docs);
});

What does err and doc mean in cursor.toArray(function(err, docs){})?


Solution

  • docs is an array with all the documents returned by the cursor.

    When there was an error, err is an object which describes that error.