MongoDB request DSL • opensavvy.ktmongo.dsl.aggregation • AggregationOperators
AggregationOperators¶
interface AggregationOperators : ValueOperators, ArrayValueOperators, ComparisonValueOperators, ConditionalValueOperators, ArithmeticValueOperators, StringValueOperators, TrigonometryValueOperators, TypeValueOperators
DSL to instantiate aggregation values, available in most aggregation stages.
What are aggregation values?¶
In MongoDB, operators targeting regular queries and aggregation pipelines often have the same name but a different syntax. Using KtMongo, operators keep the same name and syntax for both usages, but the way they are used in practice is still quite different. For example, compare $eq` (query)`](../../opensavvy.ktmongo.dsl.query/-filter-query/eq.html) and [
$eq(aggregation)
.
In regular queries, operators do not have a return type, and invoking them immediately adds them to the current query. If multiple operators are called within the operation lambda, each of them is added to the query: operators "bind" themselves on invoking. Operators always take a field
as first operand, and value as second operand. As an example:
which generates:
In aggregations, operators have a return type and only the last value of a block is taken into account, just like in regular Kotlin code. We can use regular variables to store parts of a more complex expression. Operators accept multiple aggregation values which must conform to some type requirements. As an example:
users.find {
expr { // Use aggregation values in a regular find() request
val maxScore = of(User::scores).max()
maxScore lt of(15)
}
}
which generates:
As you can see, we use the of
method to convert from Kotlin values or from field names to aggregation values. Because each side of an operator accepts an aggregation value, we can thus compare multiple fields from the same document, use conditionals or other complex requests.
In this example, we used the $expr
query predicate to write an aggregation value within a regular query. $expr
requires returning a boolean, so the last operator of the value needed to be a boolean-returning operator, which $lt
is one of. In other contexts, aggregation values can be typed with any other document type.
When writing your first aggregation pipelines, keep in mind that only the last value of each lambda is used, just like when calling Kotlin functions that return a value that is unused.
Operators¶
Access values:
Conditionally compute values:
Compare values:
Arithmetic operators:
Array operators:
Document operators:
String operators:
Type operators:
Trigonometric operators and angle management:
See also¶
Value
Representation of an aggregation value.
Inheritors¶
Properties¶
context
¶
abstract val context:
field
¶
Converts a Kotlin property into a Field
.
isArray
¶
Determines if this value is an array.
isNumber
¶
Determines if this value is a number.
length
¶
Returns the number of code points in the specified string.
lengthUTF8
¶
Returns the number of UTF-8 encoded bytes in the specified string.
type
¶
Gets the BsonType of the current value.
Functions¶
abs
¶
The absolute value of a number.
acos
¶
The inverse cosine (arc cosine) of a value, in radians.
acosh
¶
The inverse hyperbolic cosine (hyperbolic arc cosine) of a value, in radians.
asin
¶
The inverse sine (arc sine) of a value, in radians.
asinh
¶
The inverse hyperbolic sine (hyperbolic arc sine) of a value, in radians.
atan
¶
The inverse tangent (arc tangent) of a value, in radians.
atanh
¶
The inverse hyperbolic tangent (hyperbolic arc tangent) of a value, in radians.
ceil
¶
The smallest integer greater than or equal to the specified value
.
concat
¶
Concatenates two strings together.
cond
¶
Decides between two values
depending on the evaluation of a boolean value.
cos
¶
The cosine of a value that is measured in radians.
cosh
¶
The hyperbolic cosine of a value that is measured in radians.
div
¶
Divides one aggregation value by another.
Refers to field
as a nested field of the current value.
Refers to field
as a nested field of the current value.
Refers to child
as a nested field of the current field.
eq
¶
Compares two aggregation values and returns true
if they are equivalent.
filter
¶
Selects a subset of an array to return based on the specified predicate
, similarly to kotlin.collections.filter.
Selects a subset of an array to return based on the specified predicate
, similarly to kotlin.collections.filter.
Selects a subset of an array to return based on the specified predicate
, similarly to kotlin.collections.filter.
Selects a subset of an array to return based on the specified predicate
, similarly to kotlin.collections.filter.
floor
¶
The largest integer less than or equal to the specified value
.
get
¶
Refers to a specific item in an array, by its index.
Refers to a specific item in a map, by its name.
gt
¶
gte
¶
lowercase
¶
Converts a string to lowercase, returning the result.
lt
¶
lte
¶
map
¶
Applies a transform
to all elements in an array and returns the array with the applied results, similar to kotlin.collections.map.
Applies a transform
to all elements in an array and returns the array with the applied results, similar to kotlin.collections.map.
Applies a transform
to all elements in an array and returns the array with the applied results, similar to kotlin.collections.map.
Applies a transform
to all elements in an array and returns the array with the applied results, similar to kotlin.collections.map.
minus
¶
Subtracts one aggregation value from another.
ne
¶
Compares two aggregation values and returns true
if they are not equivalent.
of
¶
Refers to a Kotlin value
within an aggregation value
.
Refers to a BsonType within an aggregation value
.
Refers to a field
within an aggregation value
.
Refers to a field
within an aggregation value
.
plus
¶
Sums two aggregation values.
replace
¶
Replaces all instances of find
with a replacement
string.
Replaces all instances of find
with a replacement
string.
replaceFirst
¶
Replaces the first instance of find
with a replacement
string.
Replaces the first instance of find
with a replacement
string.
sin
¶
The sine of a value that is measured in radians.
sinh
¶
The hyperbolic sine of a value that is measured in radians.
sorted
¶
Sorts an array based on its elements, in ascending order.
Sorts an array based on its elements, in ascending order.
Sorts an array based on its elements, in ascending order.
Sorts an array based on its elements, in ascending order.
sortedBy
¶
Sorts an array based on fields of its elements.
Sorts an array based on fields of its elements.
Sorts an array based on fields of its elements.
Sorts an array based on fields of its elements.
sortedDescending
¶
open fun <Context : Any, T> Collection<T>.sortedDescending(): Value<Context, List<T>>
Sorts an array based on its elements, in descending order.
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.sortedDescending(): Value<Context, List<T>>
Sorts an array based on its elements, in descending order.
Sorts an array based on its elements, in descending order.
Sorts an array based on its elements, in descending order.
split
¶
Divides a string into an array of substrings based on a delimiter
.
Divides a string into an array of substrings based on a delimiter
.
substring
¶
Returns the substring of a string.
Returns the substring of a string.
substringUTF8
¶
Returns the substring of a string.
Returns the substring of a string.
switch
¶
Selects one value based on multiple conditions.
take
¶
Returns the first limit
elements in an array, similar to kotlin.collections.take.
Returns the first limit
elements in an array, similar to kotlin.collections.take.
Returns the first limit
elements in an array, similar to kotlin.collections.take.
Returns the first limit
elements in an array, similar to kotlin.collections.take.
takeLast
¶
Returns the last limit
elements in an array, similar to kotlin.collections.takeLast.
Returns the last limit
elements in an array, similar to kotlin.collections.takeLast.
Returns the last limit
elements in an array, similar to kotlin.collections.takeLast.
Returns the last limit
elements in an array, similar to kotlin.collections.takeLast.
tan
¶
The tangent of a value that is measured in radians.
tanh
¶
The hyperbolic tangent of a value that is measured in radians.
times
¶
Multiplies two or more aggregation values.
toBoolean
¶
Converts this value to a BsonType.Boolean.
toDegrees
¶
Converts an angle in radians to an angle in degrees.
toDouble
¶
Converts this value to a BsonType.Double.
toInstant
¶
Converts this value to an Instant (BsonType.Datetime).
toInt
¶
Converts this value to an Int
(BsonType.Int32).
toLong
¶
Converts this value to an Long
(BsonType.Int64).
toObjectId
¶
Converts this value to an ObjectId.
toRadians
¶
Converts an angle in degrees to an angle in radians.
toText
¶
Converts this value to a String
.
toUuid
¶
Converts a string value to a Uuid (BsonType.BinaryData).
trim
¶
Removes whitespace characters, including null, or the specified characters from the beginning and end of a string.
Removes the specified characters
from the beginning and end of a string.
Removes the specified characters
from the beginning and end of a string.
trimEnd
¶
Removes whitespace characters, including null, or the specified characters from the end of a string.
Removes the specified characters
from the end of a string.
Removes the specified characters
from the end of a string.
trimStart
¶
Removes whitespace characters, including null, or the specified characters from the beginning of a string.
Removes the specified characters
from the beginning of a string.
Removes the specified characters
from the beginning of a string.
uppercase
¶
Converts a string to uppercase, returning the result.