AggregationOperators
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) 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:
users.find {
User::age gt 18
User::scores[0] eq 10
}
which generates:
{ "$and": [{ "$eq": { "age": 18 } }, { "$eq": { "scores.0": 10 } }] }
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:
{ "expr": { "lt": [{ "$max": "$scores" }, { "$literal": 15 }] } }
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:
Type operators:
Trigonometric operators and angle management:
See also
Representation of an aggregation value.
Inheritors
Functions
Refers to field as a nested field of the current value.
Refers to child as a nested field of the current field.
Selects a subset of an array to return based on the specified predicate, similarly to kotlin.collections.filter.
Refers to a specific item in an array, by its index.
Refers to a specific item in a map, by its name.
Applies a transform to all elements in an array and returns the array with the applied results, similar to kotlin.collections.map.
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.
Sorts an array based on its elements, in ascending order.
Sorts an array based on fields of its elements.
Sorts an array based on its elements, in descending order.
Returns the first limit elements in an array, similar to kotlin.collections.take.
Returns the last limit elements in an array, similar to kotlin.collections.takeLast.