Back to top

    ReQL command: branch

    Command syntax

    r.branch(test, true_action[, test2, test2_action, ...], false_action) → any

    test.branch(true_action[, test2, test2_action, ...], false_action) → any


    Perform a branching conditional equivalent to if-then-else.

    The branch command takes 2n+1 arguments: pairs of conditional expressions and commands to be executed if the conditionals return any value but False or None (i.e., “truthy” values), with a final “else” command to be evaluated if all of the conditionals are False or None.

    You may call branch infix style on the first test. (See the second example for an illustration.)

    r.branch(test1, val1, test2, val2, elseval)

    is the equivalent of the Python statement

    if test1:
        return val1
    elif test2:
        return val2
        return elseval

    Example: Test the value of x.

    x = 10
    r.branch((x > 5), 'big', 'small').run(conn)
    > "big"

    Example: As above, infix-style.

    x = 10
    r.expr(x > 5).branch('big', 'small').run(conn)
    > "big"

    Example: Categorize heroes by victory counts.

            r.row['victories'] > 100,
            r.row['name'] + ' is a superhero',
            r.row['victories'] > 10,
            r.row['name'] + ' is a hero',
            r.row['name'] + ' is very nice'

    If the documents in the table marvel are:

        { "name": "Iron Man", "victories": 214 },
        { "name": "Jubilee", "victories": 49 },
        { "name": "Slava", "victories": 5 }

    The results will be:

        "Iron Man is a superhero",
        "Jubilee is a hero",
        "Slava is very nice"

    Get more help

    Couldn't find what you were looking for?