Back to top

    ReQL command: map

    Command syntax

    sequence1.map([sequence2, ...], function) → stream

    array1.map([array2, ...], function) → array

    r.map(sequence1[, sequence2, ...], function) → stream

    r.map(array1[, array2, ...], function) → array

    Description

    Transform each element of one or more sequences by applying a mapping function to them. If map is run with two or more sequences, it will iterate for as many items as there are in the shortest sequence.

    Note that map can only be applied to sequences, not single values. If you wish to apply a function to a single value/selection (including an array), use the do command.

    Example: Return the first five squares.

    > r.expr([1, 2, 3, 4, 5]).map{ |val| (val * val) }.run(conn)
    
    [1, 4, 9, 16, 25]
    

    Example: Sum the elements of three sequences.

    > sequence1 = [100, 200, 300, 400]
    > sequence2 = [10, 20, 30, 40]
    > sequence3 = [1, 2, 3, 4]
    > r.map(sequence1, sequence2, sequence3){ |val1, val2, val3|
          (val1 + val2 + val3)
      }.run(conn)
    
    [111, 222, 333, 444]
    

    Example: Rename a field when retrieving documents using map and merge.

    This example renames the field id to user_id when retrieving documents from the table users.

    r.table('users').map{ |doc|
        doc.merge({:user_id => doc['id']}).without('id')
    }.run(conn)
    

    Example: Assign every superhero an archenemy.

    r.table('heroes').map(r.table('villains')){ |hero, villain|
        hero.merge({:villain => villain})
    }.run(conn)
    

    Get more help

    Couldn't find what you were looking for?