Skip to content

Latest commit

 

History

History
137 lines (110 loc) · 2.89 KB

ungroup.md

File metadata and controls

137 lines (110 loc) · 2.89 KB
layout api-command
language Java
permalink api/java/ungroup/
command ungroup
related_commands
group
group/
io
grouped_stream
array

Command syntax

{% apibody %} grouped_stream.ungroup() → array grouped_data.ungroup() → array {% endapibody %}

Description

Takes a grouped stream or grouped data and turns it into an array of objects representing the groups. Any commands chained after ungroup will operate on this array, rather than operating on each group individually. This is useful if you want to e.g. order the groups by the value of their reduction.

The format of the array returned by ungroup is the same as the default native format of grouped data in the JavaScript driver and Data Explorer.

Suppose that the table games has the following data:

[
    {"id": 2, "player": "Bob", "points": 15, "type": "ranked"},
    {"id": 5, "player": "Alice", "points": 7, "type": "free"},
    {"id": 11, "player": "Bob", "points": 10, "type": "free"},
    {"id": 12, "player": "Alice", "points": 2, "type": "free"}
]

Example: What is the maximum number of points scored by each player, with the highest scorers first?

r.table("games").group("player").max("points").g("points").ungroup()
 .orderBy(r.desc("reduction")).run(conn);

The result:

[
    {
        "group": "Bob",
        "reduction": 15
    },
    {
        "group": "Alice",
        "reduction": 7
    }
]

Example: Select one random player and all their games.

r.table("games").group("player").ungroup().sample(1).run(conn);

Result:

[
    {
        "group": "Bob",
        "reduction": 15
    },
    {
        "group": "Alice",
        "reduction": 7
    }
]

Note that if you didn't call ungroup, you would instead select one random game from each player:

r.table("games").group("player").sample(1).run(conn);

Result: (Note this is a JSON representation of a List<GroupedResult>; see the group documentation for more details.)

[
    {
        "group": "Alice",
        "values": [
            {"id": 5, "player": "Alice", "points": 7, "type": "free"}
        ]
    },
    {
        "group": "Bob",
        "values": [
            {"id": 11, "player": "Bob", "points": 10, "type": "free"}
        ]
    }
[

Example: Finding the arithmetic mode of an array of values:

r.expr(r.array([1,2,2,2,3,3])).group(
  row -> row
).count().ungroup().orderBy("reduction").nth(-1).bracket("group")

Result:

2

Example: Types!

r.table('games').group('player').typeOf().run(conn); // Returns "GROUPED_STREAM"
r.table('games').group('player').ungroup().typeOf().run(conn); // Returns "ARRAY"
r.table('games').group('player').avg('points').run(conn); // Returns "GROUPED_DATA"
r.table('games').group('player').avg('points').ungroup().run(conn); // Returns "ARRAY"