Back to top

ReQL command: ungroup

Command syntax

grouped_stream.ungroup() → array

grouped_data.ungroup() → array

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')['points']
   .ungroup().order_by(r.desc('reduction')).run(conn)

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" => [
            {"id" => 2, "player" => "Bob", "points" => 15, "type" => "ranked"},
            {"id" => 11, "player" => "Bob", "points" => 10, "type" => "free"}
        ]
    }
]

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:

{
    "Alice" => [
        {"id" => 5, "player" => "Alice", "points" => 7, "type" => "free"}
    ],
    "Bob" => [
        {"id" => 11, "player" => "Bob", "points" => 10, "type" => "free"}
    ]
}

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

r.expr([1,2,2,2,3,3]).group(){|row| row}.count().ungroup().order_by('reduction').nth(-1)['group'].run(conn)

Result:

2

Example: Types!

r.table('games').group('player').type_of().run(conn) # Returns "GROUPED_STREAM"
r.table('games').group('player').ungroup().type_of().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"

Get more help

Couldn't find what you were looking for?