W3cubDocs

/RethinkDB Java

ReQL command: max

Command syntax

sequence.max([field | function]) → element
r.max(sequence, [field | function]) → element

Description

Finds the maximum element of a sequence.

The max command can be called with:

  • a field name, to return the element of the sequence with the largest value in that field;
  • a function, to apply the function to every element within the sequence and return the element which returns the largest value from the function, ignoring any elements where the function produces a non-existence error;
  • an index (the primary key or a secondary index) via optArg, to return the element of the sequence with the largest value in that index.

For more information on RethinkDB’s sorting order, read the section in ReQL data types.

Calling max on an empty sequence will throw a non-existence error; this can be handled using the default_ command.

Example: Return the maximum value in the list [3, 5, 7].

r.expr(r.array(3, 5, 7)).max().run(conn);

Example: Return the user who has scored the most points.

r.table("users").max("points").run(conn);

Example: The same as above, but using a secondary index on the points field.

r.table("users").max().optArg("index", "points").run(conn);

Example: Return the user who has scored the most points, adding in bonus points from a separate field using a function.

r.table("users").max(
    user -> user.g("points").add(user.g("bonus_points"))
).run(conn);

Example: Return the highest number of points any user has ever scored. This returns the value of that points field, not a document.

r.table("users").max("points").g("points").run(conn);

Example: Return the user who has scored the most points, but add a default None return value to prevent an error if no user has ever scored points.

r.table("users").max("points").default_(null).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/max/