ungroup

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").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"

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/ungroup/

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部