GitHub

layout api-command
language JavaScript
permalink api/javascript/each/
command each
io

cursor

undefined

related_commands
next toArray close (cursor)

next/

to_array/

close-cursor/

Command syntax

{% apibody %} cursor.each(callback[, onFinishedCallback]) array.each(callback[, onFinishedCallback]) feed.each(callback) {% endapibody %}

Description

Lazily iterate over the result set one element at a time. The second callback is optional and is called when the iteration stops (when there are no more rows or when the callback returns false).

Example: Let's process all the elements!

cursor.each(function(err, row) {
    if (err) throw err;
    processRow(row);
});

Example: If we need to know when the iteration is complete, each also accepts a second onFinished callback.

cursor.each(function(err, row) {
        if (err) throw err;
        processRow(row);
    }, function() {
        doneProcessing();
    }
);

Example: Iteration can be stopped prematurely by returning false from the callback. For instance, if you want to stop the iteration as soon as row is negative:

cursor.each(function(err, row) {
    if (err) throw err;
    if (row < 0) {
        cursor.close()
        return false;
    }
    else {
        processRow(row)
    }
});

Note: You need to manually close the cursor if you prematurely stop the iteration.

Read the original on github.com ↗