W3cubDocs

/RethinkDB Java

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(r.array(1, 2, 3, 4, 5)).map(val -> r.mul(val, val)).run(conn);

// Result:
[1, 4, 9, 16, 25]

Example: Sum the elements of three sequences.

int[] sequence1 = { 100, 200, 300, 400 };
int[] sequence2 = { 10, 20, 30, 40 };
int[] sequence3 = { 1, 2, 3, 4 };
r.map(sequence1, sequence2, sequence3,
    (val1, val2, val3) -> r.add(val1, val2).add(val3)
).run(conn);

// Result:
[111, 222, 333, 444]

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

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

r.table("users").map(
    doc -> doc.merge(r.hashMap("user_id", doc.g("id"))).without("id")
).run(conn);

Example: Assign every superhero an archenemy.

r.table("heroes").map(r.table("villains"),
    (hero, villain) -> hero.merge(r.hashMap("villain", villain))
).run(conn);

Related commands

Get more help

Couldn't find what you were looking for?

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