Skip to content

ComparisonValueOperators

Operators to compare two values.

To learn more about aggregation operators, view 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

div

open operator fun <Context : Any, Root, Child> Value<Context, 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>

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

eq

open infix fun <Context : Any, Result> Value<Context, Result>.eq(other: Value<Context, Result>): Value<Context, Boolean>

@JvmName(name = "eqByField")
open infix fun <Context : Any, Result> Value<Context, Result>.eq(other: Field<Context, Result>): Value<Context, Boolean>

@JvmName(name = "eqByProperty")
open infix fun <Context : Any, Result> Value<Context, Result>.eq(other: KProperty1<Context, Result>): Value<Context, Boolean>

infix inline fun <Context : Any, Result> Value<Context, Result>.eq(other: Result): Value<Context, Boolean>
@JvmName(name = "eqFieldReceiverByValue")
open infix fun <Context : Any, Result> Field<Context, Result>.eq(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "eqFieldReceiverByField")
open infix fun <Context : Any, Result> Field<Context, Result>.eq(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "eqFieldReceiverByProperty")
open infix fun <Context : Any, Result> Field<Context, Result>.eq(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "eqFieldReceiverByResult")
infix inline fun <Context : Any, Result> Field<Context, Result>.eq(other: Result): Value<Context, Boolean>
@JvmName(name = "eqPropertyReceiverByValue")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.eq(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "eqPropertyReceiverByField")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.eq(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "eqPropertyReceiverByProperty")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.eq(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "eqPropertyReceiverByResult")
infix inline fun <Context : Any, Result> KProperty1<Context, Result>.eq(other: Result): Value<Context, Boolean>
@JvmName(name = "eqResultReceiverByValue")
infix inline fun <Context : Any, Result> Result.eq(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "eqResultReceiverByField")
infix inline fun <Context : Any, Result> Result.eq(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "eqResultReceiverByProperty")
infix inline fun <Context : Any, Result> Result.eq(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "eqResultReceiverByResult")
infix inline fun <Context : Any, Result> Result.eq(other: Result): Value<Context, Boolean>

Compares two aggregation values and returns true if they are equivalent.

Example

class Product(
    val name: String,
    val creationDate: Instant,
    val releaseDate: Instant,
)

val releasedOnCreation = collection.aggregate()
    .match {
        expr {
            Product::creationDate eq Product::releaseDate
        }
    }
    .toList()

External resources

See also

  • ne: Negation of this operator.

  • FilterQuery.eq: Equivalent operator in regular queries.

get

@JvmName(name = "bsonField")
operator fun <Context : Any, Child> Value<Context, BsonDocument>.get(field: String): Value<Context, Child>

@JvmName(name = "bsonItem")
operator fun <Context : Any, Child> Value<Context, BsonArray>.get(index: Int): Value<Context, Child>

Refers to a child field of a field of type BsonDocument.

Example

class User(
    val _id: ObjectId,
    val name: String,
    val externalData: BsonDocument,
)

users.find {
    User::externalData.get<Profile?>("profile") ne null
}
open operator fun <Context : Any, Result> Value<Context, Collection<Result>>.get(index: Value<Context, Int>): Value<Context, Result>

open operator fun <Context : Any, Result> Value<Context, Collection<Result>>.get(index: Int): Value<Context, Result>

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

Examples

class Pet(
    val name: String,
    val age: Int,
)

class User(
    val pets: List<Pet>,
    val favorite: Pet,
)

users.aggregate()
    .set {
        User::favorite set User::pets[0] / Pet::name
    }

External resources

gt

open infix fun <Context : Any, Result> Value<Context, Result>.gt(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtByField")
open infix fun <Context : Any, Result> Value<Context, Result>.gt(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtByProperty")
open infix fun <Context : Any, Result> Value<Context, Result>.gt(other: KProperty1<Context, Result>): Value<Context, Boolean>
infix inline fun <Context : Any, Result> Value<Context, Result>.gt(other: Result): Value<Context, Boolean>
@JvmName(name = "gtFieldReceiverByValue")
open infix fun <Context : Any, Result> Field<Context, Result>.gt(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtFieldReceiverByField")
open infix fun <Context : Any, Result> Field<Context, Result>.gt(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtFieldReceiverByProperty")
open infix fun <Context : Any, Result> Field<Context, Result>.gt(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtFieldReceiverByResult")
infix inline fun <Context : Any, Result> Field<Context, Result>.gt(other: Result): Value<Context, Boolean>
@JvmName(name = "gtPropertyReceiverByValue")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.gt(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtPropertyReceiverByField")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.gt(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtPropertyReceiverByProperty")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.gt(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtPropertyReceiverByResult")
infix inline fun <Context : Any, Result> KProperty1<Context, Result>.gt(other: Result): Value<Context, Boolean>
@JvmName(name = "gtResultReceiverByValue")
infix inline fun <Context : Any, Result> Result.gt(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtResultReceiverByField")
infix inline fun <Context : Any, Result> Result.gt(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtResultReceiverByProperty")
infix inline fun <Context : Any, Result> Result.gt(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtResultReceiverByResult")
infix inline fun <Context : Any, Result> Result.gt(other: Result): Value<Context, Boolean>

gte

@JvmName(name = "gteByField")
open infix fun <Context : Any, Result> Value<Context, Result>.gte(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gteByProperty")
open infix fun <Context : Any, Result> Value<Context, Result>.gte(other: KProperty1<Context, Result>): Value<Context, Boolean>
infix inline fun <Context : Any, Result> Value<Context, Result>.gte(other: Result): Value<Context, Boolean>
@JvmName(name = "gteFieldReceiverByValue")
open infix fun <Context : Any, Result> Field<Context, Result>.gte(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gteFieldReceiverByField")
open infix fun <Context : Any, Result> Field<Context, Result>.gte(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gteFieldReceiverByProperty")
open infix fun <Context : Any, Result> Field<Context, Result>.gte(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gteFieldReceiverByResult")
infix inline fun <Context : Any, Result> Field<Context, Result>.gte(other: Result): Value<Context, Boolean>
@JvmName(name = "gtePropertyReceiverByValue")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.gte(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtePropertyReceiverByField")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.gte(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtePropertyReceiverByProperty")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.gte(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gtePropertyReceiverByResult")
infix inline fun <Context : Any, Result> KProperty1<Context, Result>.gte(other: Result): Value<Context, Boolean>
@JvmName(name = "gteResultReceiverByValue")
infix inline fun <Context : Any, Result> Result.gte(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gteResultReceiverByField")
infix inline fun <Context : Any, Result> Result.gte(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gteResultReceiverByProperty")
infix inline fun <Context : Any, Result> Result.gte(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "gteResultReceiverByResult")
infix inline fun <Context : Any, Result> Result.gte(other: Result): Value<Context, Boolean>

lt

open infix fun <Context : Any, Result> Value<Context, Result>.lt(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltByField")
open infix fun <Context : Any, Result> Value<Context, Result>.lt(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltByProperty")
open infix fun <Context : Any, Result> Value<Context, Result>.lt(other: KProperty1<Context, Result>): Value<Context, Boolean>
infix inline fun <Context : Any, Result> Value<Context, Result>.lt(other: Result): Value<Context, Boolean>
@JvmName(name = "ltFieldReceiverByValue")
open infix fun <Context : Any, Result> Field<Context, Result>.lt(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltFieldReceiverByField")
open infix fun <Context : Any, Result> Field<Context, Result>.lt(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltFieldReceiverByProperty")
open infix fun <Context : Any, Result> Field<Context, Result>.lt(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltFieldReceiverByResult")
infix inline fun <Context : Any, Result> Field<Context, Result>.lt(other: Result): Value<Context, Boolean>
@JvmName(name = "ltPropertyReceiverByValue")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.lt(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltPropertyReceiverByField")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.lt(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltPropertyReceiverByProperty")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.lt(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltPropertyReceiverByResult")
infix inline fun <Context : Any, Result> KProperty1<Context, Result>.lt(other: Result): Value<Context, Boolean>
@JvmName(name = "ltResultReceiverByValue")
infix inline fun <Context : Any, Result> Result.lt(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltResultReceiverByField")
infix inline fun <Context : Any, Result> Result.lt(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltResultReceiverByProperty")
infix inline fun <Context : Any, Result> Result.lt(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltResultReceiverByResult")
infix inline fun <Context : Any, Result> Result.lt(other: Result): Value<Context, Boolean>

lte

@JvmName(name = "lteByField")
open infix fun <Context : Any, Result> Value<Context, Result>.lte(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "lteByProperty")
open infix fun <Context : Any, Result> Value<Context, Result>.lte(other: KProperty1<Context, Result>): Value<Context, Boolean>
infix inline fun <Context : Any, Result> Value<Context, Result>.lte(other: Result): Value<Context, Boolean>
@JvmName(name = "lteFieldReceiverByValue")
open infix fun <Context : Any, Result> Field<Context, Result>.lte(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "lteFieldReceiverByField")
open infix fun <Context : Any, Result> Field<Context, Result>.lte(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "lteFieldReceiverByProperty")
open infix fun <Context : Any, Result> Field<Context, Result>.lte(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "lteFieldReceiverByResult")
infix inline fun <Context : Any, Result> Field<Context, Result>.lte(other: Result): Value<Context, Boolean>
@JvmName(name = "ltePropertyReceiverByValue")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.lte(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltePropertyReceiverByField")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.lte(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltePropertyReceiverByProperty")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.lte(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "ltePropertyReceiverByResult")
infix inline fun <Context : Any, Result> KProperty1<Context, Result>.lte(other: Result): Value<Context, Boolean>
@JvmName(name = "lteResultReceiverByValue")
infix inline fun <Context : Any, Result> Result.lte(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "lteResultReceiverByField")
infix inline fun <Context : Any, Result> Result.lte(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "lteResultReceiverByProperty")
infix inline fun <Context : Any, Result> Result.lte(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "lteResultReceiverByResult")
infix inline fun <Context : Any, Result> Result.lte(other: Result): Value<Context, Boolean>

ne

open infix fun <Context : Any, Result> Value<Context, Result>.ne(other: Value<Context, Result>): Value<Context, Boolean>

@JvmName(name = "neByField")
open infix fun <Context : Any, Result> Value<Context, Result>.ne(other: Field<Context, Result>): Value<Context, Boolean>

@JvmName(name = "neByProperty")
open infix fun <Context : Any, Result> Value<Context, Result>.ne(other: KProperty1<Context, Result>): Value<Context, Boolean>

infix inline fun <Context : Any, Result> Value<Context, Result>.ne(other: Result): Value<Context, Boolean>
@JvmName(name = "neFieldReceiverByValue")
open infix fun <Context : Any, Result> Field<Context, Result>.ne(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "neFieldReceiverByField")
open infix fun <Context : Any, Result> Field<Context, Result>.ne(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "neFieldReceiverByProperty")
open infix fun <Context : Any, Result> Field<Context, Result>.ne(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "neFieldReceiverByResult")
infix inline fun <Context : Any, Result> Field<Context, Result>.ne(other: Result): Value<Context, Boolean>
@JvmName(name = "nePropertyReceiverByValue")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.ne(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "nePropertyReceiverByField")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.ne(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "nePropertyReceiverByProperty")
open infix fun <Context : Any, Result> KProperty1<Context, Result>.ne(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "nePropertyReceiverByResult")
infix inline fun <Context : Any, Result> KProperty1<Context, Result>.ne(other: Result): Value<Context, Boolean>
@JvmName(name = "neResultReceiverByValue")
infix inline fun <Context : Any, Result> Result.ne(other: Value<Context, Result>): Value<Context, Boolean>
@JvmName(name = "neResultReceiverByField")
infix inline fun <Context : Any, Result> Result.ne(other: Field<Context, Result>): Value<Context, Boolean>
@JvmName(name = "neResultReceiverByProperty")
infix inline fun <Context : Any, Result> Result.ne(other: KProperty1<Context, Result>): Value<Context, Boolean>
@JvmName(name = "neResultReceiverByResult")
infix inline fun <Context : Any, Result> Result.ne(other: Result): Value<Context, Boolean>

Compares two aggregation values and returns true if they are not equivalent.

Example

class Product(
    val name: String,
    val creationDate: Instant,
    val releaseDate: Instant,
)

val notReleasedOnCreation = collection.aggregate()
    .match {
        expr {
            Product::creationDate ne Product::releaseDate
        }
    }
    .toList()

External resources

See also

  • eq: Negation of this operator.

  • FilterQuery.ne: Equivalent operator in regular queries.

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)
    }
}

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.

unsafeCast

Overwrites the represented type of this value.

This method can be useful to bypass type checks.

Example

If a field has changed type over time, the DTO will use the newer type. However, you may still want to write a query using the old type:

class User(
    val _id: ObjectId,
    val name: String,
    val age: Int,
)

users.filter { User::age hasType BsonType.Double }
    .updateManyWithPipeline {
        set {
            User::age set User::age.unsafeCast<Double>() // Reflect the old type
               .toInt() // Convert to the new type
        }
    }