Back to top

ReQL command: forEach

Command syntax

result.forEach(doc -> { ... })

Description

Lazily iterate over a result set one element at a time.

RethinkDB results can be iterated through via the Java Iterable and Iterator interfaces; use standard Java commands like for loops to access each item in the sequence.

Example: Let’s process all the elements!

try (Result<Object> result = r.table("users").run(conn)) {
    result.forEach(doc -> { System.out.println(doc); });
}

Example: Stop the iteration prematurely.

try (Result<Object> result = r.table("users").run(conn)) {
    for (Object doc: result) {
        if (!processRow(doc)) {
            break;
        }
    }
}

Get more help

Couldn't find what you were looking for?