table.order_by([key | function], index=index_name) → table_slice selection.order_by(key | function[, ...]) → selection<array> sequence.order_by(key | function[, ...]) → array
Sort the sequence by document values of the given key(s). To specify the ordering, wrap the attribute with either r.asc
or r.desc
(defaults to ascending).
Note: RethinkDB uses byte-wise ordering for orderBy
and does not support Unicode collations; non-ASCII characters will be sorted by UTF-8 codepoint. For more information on RethinkDB’s sorting order, read the section in ReQL data types.
Sorting without an index requires the server to hold the sequence in memory, and is limited to 100,000 documents (or the setting of the arrayLimit
option for run). Sorting with an index can be done on arbitrarily large tables, or after a between command using the same index. This applies to both secondary indexes and the primary key (e.g., index='id'
).
Sorting functions passed to order_by
must be deterministic. You cannot, for instance, order rows using the random command. Using a non-deterministic function with order_by
will raise a ReqlQueryLogicError
.
Example: Order all the posts using the index date
.
r.table('posts').order_by(index='date').run(conn)
The index must either be the primary key or have been previously created with index_create.
r.table('posts').index_create('date').run(conn)
You can also select a descending ordering:
r.table('posts').order_by(index=r.desc('date')).run(conn, callback)
Example: Order a sequence without an index.
r.table('posts').get(1)['comments'].order_by('date')
You can also select a descending ordering:
r.table('posts').get(1)['comments'].order_by(r.desc('date'))
If you’re doing ad-hoc analysis and know your table won’t have more then 100,000 elements (or you’ve changed the setting of the arrayLimit
option for run) you can run order_by
without an index:
r.table('small_table').order_by('date')
Example: You can efficiently order using multiple fields by using a compound index.
Order by date and title.
r.table('posts').order_by(index='date_and_title').run(conn)
The index must either be the primary key or have been previously created with index_create.
r.table('posts').index_create('date_and_title', lambda post: [post["date"], post["title"]]).run(conn)
Note: You cannot specify multiple orders in a compound index. See issue #2306 to track progress.
Example: If you have a sequence with fewer documents than the array_limit
, you can order it by multiple fields without an index.
r.table('small_table').order_by('date', r.desc('title'))
Example: Notice that an index ordering always has highest precedence. The following query orders posts by date, and if multiple posts were published on the same date, they will be ordered by title.
r.table('post').order_by('title', index='date').run(conn)
Example: You can use nested field syntax to sort on fields from subdocuments. (You can also create indexes on nested fields using this syntax with index_create
.)
r.table('user').order_by(lambda user: user['group']['id']).run(conn)
Example: You can efficiently order data on arbitrary expressions using indexes.
r.table('posts').order_by(index='votes').run(conn)
The index must have been previously created with index_create.
r.table('posts').index_create('votes', lambda post: post["upvotes"]-post["downvotes"] ).run(conn)
Example: If you have a sequence with fewer documents than the array_limit
, you can order it with an arbitrary function directly.
r.table('small_table').order_by(lambda doc: doc['upvotes']-doc['downvotes'] );
You can also select a descending ordering:
r.table('small_table').order_by(r.desc(lambda doc: doc['upvotes']-doc['downvotes'] ));
Example: Ordering after a between
command can be done as long as the same index is being used.
r.table("posts").between(r.time(2013, 1, 1, '+00:00'), r.time(2013, 1, 1, '+00:00'), index='date') .order_by(index='date').run(conn);
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/python/order_by/