Back to top

ReQL command: do_

Command syntax

any.do_(function) → any

r.do_([args]*, function) → any

any.do_(expr) → any

r.do_([args]*, expr) → any


Call an anonymous function using return values from other ReQL commands or queries as arguments.

The last argument to do_ (or, in some forms, the only argument) is an expression or an anonymous function which receives values from either the previous arguments or from prefixed commands chained before do_. The do_ command is essentially a single-element map, letting you map a function over just one document. This allows you to bind a query result to a local variable within the scope of do_, letting you compute the result just once and reuse it in a complex expression or in a series of ReQL commands.

Arguments passed to the do_ function must be basic data types, and cannot be streams or selections. (Read about ReQL data types.) While the arguments will all be evaluated before the function is executed, they may be evaluated in any order, so their values should not be dependent on one another. The type of do_’s result is the type of the value returned from the function or last expression.

Example: Compute a golfer’s net score for a game.

    player -> player.g("gross_score").sub(player.g("course_handicap"))

Example: Return the best scoring player in a two-player golf match.

r.do_(r.table("players").get(id1), r.table("players").get(id2),
    (player1, player2) -> r.branch(

Note that branch, the ReQL conditional command, must be used instead of if. See the branch documentation for more.

Example: Take different actions based on the result of a ReQL insert command.

import com.rethinkdb.model.MapObject;

MapObject newData = r.hashMap("id", 100)
    .with("name", "Agatha")
    .with("gross_score", 57)
    .with("course_handicap", 4);

r.table("players").insert(newData).do_(doc ->
               .with("response", doc)
               .with("result", "ok")),
               .with("response", doc)
               .with("result", "error"))

Get more help

Couldn't find what you were looking for?