Exrethinkdb.Query.Aggregation

ReQL methods for aggregation operations.

All examples assume that use Exrethinkdb has been called.

Summary

avg(arg)

Averages all the elements of a sequence. If called with a field name, averages all the values of that field in the sequence, skipping elements of the sequence that lack that field. If called with a function, calls that function on every element of the sequence and averages the results, skipping elements of the sequence where that function returns None or a non-existence error

avg(left, right)
contains(seq, args)

When called with values, returns true if a sequence contains all the specified values. When called with predicate functions, returns true if for each predicate there exists at least one element of the stream where that predicate returns true

count(arg)

Counts the number of elements in a sequence. If called with a value, counts the number of times that value occurs in the sequence. If called with a predicate function, counts the number of elements in the sequence where that function returns true

count(left, right)
distinct(arg)
distinct(seq, opts)

Removes duplicates from elements in a sequence

group(seq, keys)

Takes a stream and partitions it into multiple groups based on the fields or functions provided

max(arg)
max(seq, opts)

Finds the maximum element of a sequence. The max command can be called with:

min(arg)
min(seq, opts)

Finds the minimum element of a sequence. The min command can be called with:

reduce(left, right)

Produce a single value from a sequence through repeated application of a reduction function

sum(arg)

Sums all the elements of a sequence. If called with a field name, sums all the values of that field in the sequence, skipping elements of the sequence that lack that field. If called with a function, calls that function on every element of the sequence and sums the results, skipping elements of the sequence where that function returns nil or a non-existence error

sum(left, right)
ungroup(arg)

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

Functions

avg(arg)

Specs:

Averages all the elements of a sequence. If called with a field name, averages all the values of that field in the sequence, skipping elements of the sequence that lack that field. If called with a function, calls that function on every element of the sequence and averages the results, skipping elements of the sequence where that function returns None or a non-existence error.

Produces a non-existence error when called on an empty sequence. You can handle this case with default.

avg(left, right)

Specs:

contains(seq, args)

Specs:

When called with values, returns true if a sequence contains all the specified values. When called with predicate functions, returns true if for each predicate there exists at least one element of the stream where that predicate returns true.

count(arg)

Specs:

Counts the number of elements in a sequence. If called with a value, counts the number of times that value occurs in the sequence. If called with a predicate function, counts the number of elements in the sequence where that function returns true.

If count is called on a binary object, it will return the size of the object in bytes.

count(left, right)

Specs:

distinct(arg)
distinct(seq, opts)

Specs:

Removes duplicates from elements in a sequence.

The distinct command can be called on any sequence, a table, or called on a table with an index.

group(seq, keys)

Specs:

Takes a stream and partitions it into multiple groups based on the fields or functions provided.

With the multi flag single documents can be assigned to multiple groups, similar to the behavior of multi-indexes. When multi is True and the grouping value is an array, documents will be placed in each group that corresponds to the elements of the array. If the array is empty the row will be ignored.

max(arg)
max(seq, opts)

Specs:

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 smallest value in that field;
  • an index, to return the element of the sequence with the smallest value in that index;
  • a function, to apply the function to every element within the sequence and return the element which returns the smallest value from the function, ignoring any elements where the function returns None or produces a non-existence error.

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

min(arg)
min(seq, opts)

Specs:

Finds the minimum element of a sequence. The min command can be called with:

  • a field name, to return the element of the sequence with the smallest value in that field;
  • an index, to return the element of the sequence with the smallest value in that index;
  • a function, to apply the function to every element within the sequence and return the element which returns the smallest value from the function, ignoring any elements where the function returns None or produces a non-existence error.

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

reduce(left, right)

Specs:

Produce a single value from a sequence through repeated application of a reduction function.

The reduction function can be called on:

  • two elements of the sequence
  • one element of the sequence and one result of a previous reduction
  • two results of previous reductions

The reduction function can be called on the results of two previous reductions because the reduce command is distributed and parallelized across shards and CPU cores. A common mistaken when using the reduce command is to suppose that the reduction is executed from left to right.

sum(arg)

Specs:

Sums all the elements of a sequence. If called with a field name, sums all the values of that field in the sequence, skipping elements of the sequence that lack that field. If called with a function, calls that function on every element of the sequence and sums the results, skipping elements of the sequence where that function returns nil or a non-existence error.

Returns 0 when called on an empty sequence.

sum(left, right)

Specs:

ungroup(arg)

Specs:

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. end