Skip to content

ArrayValueOperators

Operators to manipulate arrays.

To learn more about aggregation operators, see opensavvy.ktmongo.dsl.aggregation.AggregationOperators.

Inheritors

Properties

context

@LowLevelApi
abstract override val context: BsonContext

The strategy used when converting from KProperty1 to Field.

field

Converts a Kotlin property into a Field.

Functions

average

@JvmName(name = "averageFieldReceiver")
open fun <Context : Any, T : Number> Field<Context, Collection<Number?>>.average(): Value<Context, T>

@JvmName(name = "averagePropertyReceiver")
open fun <Context : Any, T : Number> KProperty1<Context, Collection<Number?>>.average(): Value<Context, T>

@JvmName(name = "averageResultReceiver")
open fun <Context : Any, T : Number> Collection<Number?>.average(): Value<Context, T>
open fun <Context : Any, T : Number> average(vararg input: Value<Context, Number?>): Value<Context, T>
@JvmName(name = "averageByField")
open fun <Context : Any, T : Number> average(vararg input: Field<Context, Number?>): Value<Context, T>
@JvmName(name = "averageByProperty")
open fun <Context : Any, T : Number> average(vararg input: KProperty1<Context, Number?>): Value<Context, T>

Returns the average of the elements in the array.

Example

class Player(
    val _id: ObjectId,
    val scores: List<Int>,
    val averageScore: Double,
)

players.updateManyWithPipeline {
    set {
        Player::averageScore set Player::scores.average()
    }
}

External resources

open fun <Context : Any, T : Number> average(): Value<Context, T>

Deprecated (with error)

Computing the average of 0 elements makes no sense, you should specify the elements to average as the receiver or as arguments.

div

open operator fun <Context : Any, Root, Child> Value<Context, Root>.div(field: Field<Root, Child>): Value<Context, Child>

@JvmName(name = "divFieldReceiver")
open operator fun <Context : Any, Root, Child> Field<Context, Root>.div(field: Field<Root, Child>): Value<Context, Child>

@JvmName(name = "divResultReceiver")
inline operator fun <Context : Any, Root, Child> Root.div(field: Field<Root, Child>): Value<Context, Child>

open operator fun <Context : Any, Root, Child> Value<Context, Root>.div(field: KProperty1<Root, Child>): Value<Context, Child>
@JvmName(name = "divFieldReceiver")
open operator fun <Context : Any, Root, Child> Field<Context, Root>.div(field: KProperty1<Root, Child>): Value<Context, Child>
@JvmName(name = "divResultReceiver")
inline operator fun <Context : Any, Root, Child> Root.div(field: KProperty1<Root, Child>): Value<Context, Child>

Refers to field as a nested field of the current value.

Examples

class User(
    val name: String,
)

class Data(
    val users: List<User>,
    val userNames: List<Int>,
)

data.aggregate()
    .set {
        Data::userNames set Data::users.map { it / User::name }
    }

External resources

filter

open fun <Context : Any, T> Value<Context, Collection<T>>.filter(
    limit: Value<Context, Int>? = null, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>

@JvmName(name = "filterByField")
open fun <Context : Any, T> Value<Context, Collection<T>>.filter(
    limit: Field<Context, Int>, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>

@JvmName(name = "filterByProperty")
open fun <Context : Any, T> Value<Context, Collection<T>>.filter(
    limit: KProperty1<Context, Int>, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>

open fun <Context : Any, T> Value<Context, Collection<T>>.filter(
    limit: Int, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>
@JvmName(name = "filterFieldReceiverByValue")
open fun <Context : Any, T> Field<Context, Collection<T>>.filter(
    limit: Value<Context, Int>? = null, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>
@JvmName(name = "filterFieldReceiverByField")
open fun <Context : Any, T> Field<Context, Collection<T>>.filter(
    limit: Field<Context, Int>, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>
@JvmName(name = "filterFieldReceiverByProperty")
open fun <Context : Any, T> Field<Context, Collection<T>>.filter(
    limit: KProperty1<Context, Int>, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>
@JvmName(name = "filterFieldReceiverByResult")
open fun <Context : Any, T> Field<Context, Collection<T>>.filter(
    limit: Int, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>
@JvmName(name = "filterPropertyReceiverByValue")
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.filter(
    limit: Value<Context, Int>? = null, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>
@JvmName(name = "filterPropertyReceiverByField")
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.filter(
    limit: Field<Context, Int>, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>
@JvmName(name = "filterPropertyReceiverByProperty")
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.filter(
    limit: KProperty1<Context, Int>, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>
@JvmName(name = "filterPropertyReceiverByResult")
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.filter(
    limit: Int, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>
@JvmName(name = "filterResultReceiverByValue")
open fun <Context : Any, T> Collection<T>.filter(
    limit: Value<Context, Int>? = null, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>
@JvmName(name = "filterResultReceiverByField")
open fun <Context : Any, T> Collection<T>.filter(
    limit: Field<Context, Int>, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>
@JvmName(name = "filterResultReceiverByProperty")
open fun <Context : Any, T> Collection<T>.filter(
    limit: KProperty1<Context, Int>, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>
@JvmName(name = "filterResultReceiverByResult")
open fun <Context : Any, T> Collection<T>.filter(
    limit: Int, 
    variableName: String = "this", 
    predicate: AggregationOperators.(Value<Any, T>) -> Value<T & Any, Boolean>
): Value<Context, List<T>>

Selects a subset of an array to return based on the specified predicate, similarly to Kotlin'sfilter``.

The returned elements are in the original order.

Example

class Sensor(
    val measurements: List<Int>,
)

collection.updateManyWithPipeline {
    set {
        Sensor::measurements set (Sensor::measurements).filter { it gte of(0) }
    }
}

External resources

Parameters

  • limit: If set, specifies a maximum number of elements returned: only the first limit matching elements are returned, even if there are more matching elements. Must be greater or equal to 1, or be null.

  • variableName: The name of the temporary variable passed to the predicate lambda, which represents the current element being iterated over. By default, "this". Setting this parameter is only useful when using nested filter or other similar calls, which could otherwise conflict.

get

open operator fun <Root, Type> KProperty1<Root, Collection<Type>>.get(index: Int): Field<Root, Type>

Refers to a specific item in an array, by its index.

open operator fun <Root, Type> KProperty1<Root, Map<String, Type>>.get(index: String): Field<Root, Type>

Refers to a specific item in a map, by its name.

open operator fun <Root, Type> Field<Root, Collection<Type>>.get(index: Int): Field<Root, Type>

Refers to a specific item in an array, by its index.

open operator fun <Root, Type> Field<Root, Map<String, Type>>.get(key: String): Field<Root, Type>

Refers to a specific item in a map, by its name.

map

open fun <Context : Any, T, R> Value<Context, Collection<T>>.map(variableName: String = "this", transform: AggregationOperators.(Value<Any, T>) -> Value<Context, R>): Value<Context, List<R>>

@JvmName(name = "mapFieldReceiver")
open fun <Context : Any, T, R> Field<Context, Collection<T>>.map(variableName: String = "this", transform: AggregationOperators.(Value<Any, T>) -> Value<Context, R>): Value<Context, List<R>>

@JvmName(name = "mapPropertyReceiver")
open fun <Context : Any, T, R> KProperty1<Context, Collection<T>>.map(variableName: String = "this", transform: AggregationOperators.(Value<Any, T>) -> Value<Context, R>): Value<Context, List<R>>

@JvmName(name = "mapResultReceiver")
open fun <Context : Any, T, R> Collection<T>.map(variableName: String = "this", transform: AggregationOperators.(Value<Any, T>) -> Value<Context, R>): Value<Context, List<R>>

Applies a transform to all elements in an array and returns the array with the applied results, similar to kotlin.collections.map.

Example

class Player(
    val _id: ObjectId,
    val scores: List<Int>,
)

players.updateManyWithPipeline {
    set {
        Player::scores set Player::scores
            .map {
                it + of(1)
            }
    }
}

External resources

of

Refers to a field within an aggregation value.

Example

class Product(
    val acceptanceDate: Instant,
    val publishingDate: Instant,
)

val publishedBeforeAcceptance = products.find {
    expr {
        of(Product::publishingDate) lt of(Product::acceptanceDate)
    }
}
open fun <Result> of(value: Result, type: KType): Value<Any, Result>

inline fun <Result> of(value: Result): Value<Any, Result>

Refers to a Kotlin value within an aggregation value.

Example

class Product(
    val age: Int,
)

val publishedBeforeAcceptance = products.find {
    expr {
        of(Product::age) lt of(15)
    }
}
open fun of(value: BsonType): Value<Any, BsonType>

Refers to a BsonType within an aggregation value.

Example

class Product(
    val age: Int,
)

val publishedBeforeAcceptance = products.find {
    expr {
        of(Product::age).type eq of(BsonType.Int32)
    }
}

sorted

@JvmName(name = "sortedFieldReceiver")
open fun <Context : Any, T> Field<Context, Collection<T>>.sorted(): Value<Context, List<T>>

@JvmName(name = "sortedPropertyReceiver")
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.sorted(): Value<Context, List<T>>

@JvmName(name = "sortedResultReceiver")
open fun <Context : Any, T> Collection<T>.sorted(): Value<Context, List<T>>

Sorts an array based on its elements, in ascending order.

Example

class Player(
    val _id: ObjectId,
    val scores: List<Int>,
    val worstScores: List<Int>,
)

players.updateManyWithPipeline {
    set {
        Player::bestScores set Player::scores
            .sorted()
            .take(5)
    }
}

External resources

See also

sortedBy

open fun <Context : Any, T> Value<Context, Collection<T>>.sortedBy(order: SortOptionDsl<T & Any>.() -> Unit): Value<Context, List<T>>

@JvmName(name = "sortedByFieldReceiver")
open fun <Context : Any, T> Field<Context, Collection<T>>.sortedBy(order: SortOptionDsl<T & Any>.() -> Unit): Value<Context, List<T>>

@JvmName(name = "sortedByPropertyReceiver")
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.sortedBy(order: SortOptionDsl<T & Any>.() -> Unit): Value<Context, List<T>>

@JvmName(name = "sortedByResultReceiver")
open fun <Context : Any, T> Collection<T>.sortedBy(order: SortOptionDsl<T & Any>.() -> Unit): Value<Context, List<T>>

Sorts an array based on fields of its elements.

Example

class Score(
    val value: Int,
)

class Player(
    val _id: ObjectId,
    val scores: List<Score>,
    val bestScores: List<Score>,
)

players.updateManyWithPipeline {
    set {
        Player::bestScores set Player::scores
            .sortedBy { ascending(Score::value) }
    }
}

External resources

See also

  • sorted: Sort by the elements themselves (ascending order).

  • sortedDescending: Sort by the elements themselves (descending order).

sortedDescending

@JvmName(name = "sortedDescendingFieldReceiver")
open fun <Context : Any, T> Field<Context, Collection<T>>.sortedDescending(): Value<Context, List<T>>

@JvmName(name = "sortedDescendingPropertyReceiver")
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.sortedDescending(): Value<Context, List<T>>

@JvmName(name = "sortedDescendingResultReceiver")
open fun <Context : Any, T> Collection<T>.sortedDescending(): Value<Context, List<T>>

Sorts an array based on its elements, in descending order.

Example

class Player(
    val _id: ObjectId,
    val scores: List<Int>,
    val bestScores: List<Int>,
)

players.updateManyWithPipeline {
    set {
        Player::bestScores set Player::scores
            .sortedDescending()
            .take(5)
    }
}

External resources

See also

take

open fun <Context : Any, T> Value<Context, Collection<T>>.take(limit: Value<Context, Int>): Value<Context, List<T>>

@JvmName(name = "takeByField")
open fun <Context : Any, T> Value<Context, Collection<T>>.take(limit: Field<Context, Int>): Value<Context, List<T>>

@JvmName(name = "takeByProperty")
open fun <Context : Any, T> Value<Context, Collection<T>>.take(limit: KProperty1<Context, Int>): Value<Context, List<T>>

inline fun <Context : Any, T> Value<Context, Collection<T>>.take(limit: Int): Value<Context, List<T>>
@JvmName(name = "takeFieldReceiverByValue")
open fun <Context : Any, T> Field<Context, Collection<T>>.take(limit: Value<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeFieldReceiverByField")
open fun <Context : Any, T> Field<Context, Collection<T>>.take(limit: Field<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeFieldReceiverByProperty")
open fun <Context : Any, T> Field<Context, Collection<T>>.take(limit: KProperty1<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeFieldReceiverByResult")
inline fun <Context : Any, T> Field<Context, Collection<T>>.take(limit: Int): Value<Context, List<T>>
@JvmName(name = "takePropertyReceiverByValue")
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.take(limit: Value<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takePropertyReceiverByField")
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.take(limit: Field<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takePropertyReceiverByProperty")
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.take(limit: KProperty1<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takePropertyReceiverByResult")
inline fun <Context : Any, T> KProperty1<Context, Collection<T>>.take(limit: Int): Value<Context, List<T>>
@JvmName(name = "takeResultReceiverByValue")
open fun <Context : Any, T> Collection<T>.take(limit: Value<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeResultReceiverByField")
open fun <Context : Any, T> Collection<T>.take(limit: Field<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeResultReceiverByProperty")
open fun <Context : Any, T> Collection<T>.take(limit: KProperty1<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeResultReceiverByResult")
inline fun <Context : Any, T> Collection<T>.take(limit: Int): Value<Context, List<T>>

Returns the first limit elements in an array, similar to kotlin.collections.take.

Example

class Player(
    val _id: ObjectId,
    val scores: List<Int>,
    val firstScores: List<Int>,
)

players.updateManyWithPipeline {
    set {
        Player::firstScores set Player::scores.take(3)
    }
}

External resources

takeLast

@JvmName(name = "takeLastByField")
open fun <Context : Any, T> Value<Context, Collection<T>>.takeLast(limit: Field<Context, Int>): Value<Context, List<T>>

@JvmName(name = "takeLastByProperty")
open fun <Context : Any, T> Value<Context, Collection<T>>.takeLast(limit: KProperty1<Context, Int>): Value<Context, List<T>>

inline fun <Context : Any, T> Value<Context, Collection<T>>.takeLast(limit: Int): Value<Context, List<T>>
@JvmName(name = "takeLastFieldReceiverByValue")
open fun <Context : Any, T> Field<Context, Collection<T>>.takeLast(limit: Value<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeLastFieldReceiverByField")
open fun <Context : Any, T> Field<Context, Collection<T>>.takeLast(limit: Field<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeLastFieldReceiverByProperty")
open fun <Context : Any, T> Field<Context, Collection<T>>.takeLast(limit: KProperty1<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeLastFieldReceiverByResult")
inline fun <Context : Any, T> Field<Context, Collection<T>>.takeLast(limit: Int): Value<Context, List<T>>
@JvmName(name = "takeLastPropertyReceiverByValue")
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.takeLast(limit: Value<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeLastPropertyReceiverByField")
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.takeLast(limit: Field<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeLastPropertyReceiverByProperty")
open fun <Context : Any, T> KProperty1<Context, Collection<T>>.takeLast(limit: KProperty1<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeLastPropertyReceiverByResult")
inline fun <Context : Any, T> KProperty1<Context, Collection<T>>.takeLast(limit: Int): Value<Context, List<T>>
@JvmName(name = "takeLastResultReceiverByValue")
open fun <Context : Any, T> Collection<T>.takeLast(limit: Value<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeLastResultReceiverByField")
open fun <Context : Any, T> Collection<T>.takeLast(limit: Field<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeLastResultReceiverByProperty")
open fun <Context : Any, T> Collection<T>.takeLast(limit: KProperty1<Context, Int>): Value<Context, List<T>>
@JvmName(name = "takeLastResultReceiverByResult")
inline fun <Context : Any, T> Collection<T>.takeLast(limit: Int): Value<Context, List<T>>

Returns the last limit elements in an array, similar to kotlin.collections.takeLast.

Example

class Player(
    val _id: ObjectId,
    val scores: List<Int>,
    val lastScores: List<Int>,
)

players.updateManyWithPipeline {
    set {
        Player::lastScores set Player::scores.takeLast(3)
    }
}

External resources

unsafe

open infix fun <Root, Child> KProperty1<Root, *>.unsafe(child: String): Field<Root, Child>

Refers to a field child of the current field, with no compile-time safety.

open infix fun <Root, Child> KProperty1<Root, *>.unsafe(child: KProperty1<*, Child>): Field<Root, Child>

Refers to a field child of the current field, without checking that it is a field available on the current object.

open infix fun <Root, Child> KProperty1<Root, *>.unsafe(child: Field<*, Child>): Field<Root, Child>

Refers to a field child of the current field, without checking that it is a field available on the current object.

open infix fun <Root, Child> Field<Root, *>.unsafe(child: KProperty1<*, Child>): Field<Root, Child>

Refers to a field child of the current field, without checking that it is a field available on the current object.

open infix fun <Root, Child> Field<Root, *>.unsafe(child: Field<*, Child>): Field<Root, Child>

Refers to a field child of the current field, without checking that it is a field available on the current object.

open infix fun <Root, Type, Child> Field<Root, Type>.unsafe(child: String): Field<Root, Child>

Refers to a field child of the current field, with no compile-time safety.