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;
        }
    }
}

© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/api/java/each/