GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Close
  • FAQ
  • Docs
  • API
  • Community
  • Blog
  • Accessing ReQL
  • r
  • connect
  • close
  • reconnect
  • use
  • run
  • changes

This view

  • Original page
  • New address

GeezerVison is showing rethinkdb.com — ReQL command reference - RethinkDB


▲ Back to top

Command syntax

any.do(function) → any

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

any.do(expr) → any

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

Description

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.

r.table('players').get('f19b5f16-ef14-468f-bd48-e194761df255').do(
    function (player) {
        return player('gross_score').sub(player('course_handicap'));
    }
).run(conn, callback);

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

r.do(r.table('players').get(id1), r.table('players').get(id2),
    function (player1, player2) {
        return r.branch(player1('gross_score').lt(player2('gross_score')),
            player1, player2);
    }
).run(conn, callback);

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.

var newData = {
    id: 100,
    name: 'Agatha',
    gross_score: 57,
    course_handicap: 4
};
r.table('players').insert(newData).do(
    function (doc) {
        return r.branch(doc('inserted').ne(0),
            r.table('log').insert({time: r.now(), response: doc, result: 'ok'}),
            r.table('log').insert({time: r.now(), response: doc, result: 'error'}))
    }
).run(conn, callback);

Related commands

  • map

Get more help

Couldn't find what you were looking for?

  • Ask a question on Stack Overflow
  • Chat with us and our community on Slack
  • Talk to the team on IRC on #[email protected] — via Webchat
  • Ping @rethinkdb on Twitter
  • Post an issue on the documentation issue tracker on GitHub

Contribute: edit this page or open an issue

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.