sequence.max(field | function) → element
sequence.max({index: <indexname>}) → element
r.max(sequence, field | function) → element
r.max(sequence, {index: <indexname>}) → element
Finds the maximum element of a sequence.
The max
command can be called with:
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([3, 5, 7]).max().run(conn, callback);
Example: Return the user who has scored the most points.
r.table('users').max('points').run(conn, callback);
Example: The same as above, but using a secondary index on the points
field.
r.table('users').max({index: 'points'}).run(conn, callback);
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(function(user) {
return user('points').add(user('bonusPoints'));
}).run(conn, callback);
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')('points').run(conn, callback);
Example: Return the user who has scored the most points, but add a default null
return value to prevent an error if no user has ever scored points.
r.table('users').max('points').default(null).run(conn, callback);
Couldn't find what you were looking for?
Contribute: edit this page or open an issue