Skip to content

UpdateQuery

DSL for MongoDB operators that are used to update existing values (does not include aggregation operators).

Example

This expression type is available on multiple operations, most commonly update:

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

collection.update(
    filter = {
        User::name eq "Bob"
    },
    update = {
        User::age set 18
    }
)

Operators

On regular fields:

On arrays:

Time management:

If you can't find the operator you're searching for, visit the tracking issue.

External resources

See also

Inheritors

Constructors

UpdateQuery

Creates an empty UpdateQuery.

Types

PushBuilder

interface PushBuilder<V> : BsonNode

DSL builder for advanced $push operations.

PushSortDsl

interface PushSortDsl<V : Any> : SortOptionDsl<V> 

DSL for sorting elements during a $push operation.

Properties

all

open val <E> Field<T, Collection<E>>.all: Field<T, E>

open val <E> KProperty1<T, Collection<E>>.all: Field<T, E>

The all positional operator: selects all elements of an array.

This operator is used to declare an update that applies to all items of an array.

Example

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

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

users.updateMany {
    User::pets.all / Pet::age inc 1
}

External resources

context

The context used to generate this expression.

field

Converts a Kotlin property into a Field.

selected

open val <E> Field<T, Collection<E>>.selected: Field<T, E>

open val <E> KProperty1<T, Collection<E>>.selected: Field<T, E>

The positional operator: update an array item selected in the filter.

When we use any or anyValue in a filter to select an item, we can use this operator to update whichever item was selected.

Do not use this operator in an upsert.

Example

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

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

users.updateMany(
    filter = {
        User::pets.any / Pet::name eq "Bobby"
    },
    update = {
        User::pets.selected / Pet::age inc 1
    }
)

This example finds all users who have a pet named "Bobby", and increases its age by 1. Note that if the users have other pets, they are not impacted.

External resources

Functions

accept

@LowLevelApi
@DangerousMongoApi
abstract override fun accept(node: BsonNode)

Adds a new node as a child of this one.

acceptAll

Adds any number of nodes into this one.

addEachToSet

open fun <V> Field<T, Collection<V>>.addEachToSet(values: Iterable<V>, type: KType)

infix inline fun <V> Field<T, Collection<V>>.addEachToSet(values: Iterable<V>)

infix inline fun <V> KProperty1<T, Collection<V>>.addEachToSet(values: Iterable<V>)

Adds multiple values at the end of the array, unless they are already present.

Each value in values is treated independently. It if it is already present in the array, nothing happens. If it is absent from the array, it is added at the end.

MongoDB detects equality if the two documents are exactly identical. All the fields must be the same in the same order.

This is a convenience function for calling addToSet multiple times.

Example

class User(
    val name: String,
    val age: Int,
    val tokens: List<String>,
)

collection.updateOne(
    filter = {
        User::name eq "Bob"
    },
    update = {
        User::tokens addEachToSet listOf("123456789", "789456123")
    }
)

This will add "123456789" and "789465123" to the user's tokens only if they aren't already present. If only one of them is present, the other is added.

External resources

addToSet

abstract fun <V> Field<T, Collection<V>>.addToSet(value: V, type: KType)

infix inline fun <V> Field<T, Collection<V>>.addToSet(value: V)

infix inline fun <V> KProperty1<T, Collection<V>>.addToSet(value: V)

Adds value at the end of the array, unless it is already present, in which case it does nothing.

MongoDB detects equality if the two documents are exactly identical. All the fields must be the same in the same order.

Example

class User(
    val name: String,
    val age: Int,
    val tokens: List<String>,
)

collection.updateOne(
    filter = {
        User::name eq "Bob"
    },
    update = {
        User::tokens addToSet "123456789"
    }
)

This will add "123456789" to the user's tokens only if it isn't already present.

External resources

div

open operator fun <Root, Parent, Child> KProperty1<Root, Parent>.div(child: KProperty1<Parent & Any, Child>): Field<Root, Child>

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

open operator fun <Root, Type, Child> Field<Root, Type>.div(child: KProperty1<in Type & Any, Child>): Field<Root, Child>

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

open operator fun <Root, Type, Child> Field<Root, Type>.div(child: Field<Type, Child>): Field<Root, Child>

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

freeze

@LowLevelApi
abstract override fun freeze()

Makes this expression immutable.

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.

inc

abstract fun <V : Number> Field<T, V>.inc(amount: V, type: KType)

infix inline fun <V : Number> Field<T, V>.inc(amount: V)

infix inline fun <V : Number> KProperty1<T, V>.inc(amount: V)

Increments a field by the specified amount.

amount may be negative, in which case the field is decremented.

If the field doesn't exist (either the document doesn't have it, or the operation is an upsert and a new document is created), the field is created with an initial value of amount.

Use of this operator with a field with a null value will generate an error.

Example

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

// It's the new year!
collection.updateMany {
    User::age inc 1
}

External resources

max

abstract fun <V : Comparable<V>> Field<T, V?>.max(value: V, type: KType)

infix inline fun <V : Comparable<V>> Field<T, V?>.max(value: V)

infix inline fun <V : Comparable<V>> KProperty1<T, V?>.max(value: V)

Updates the value of a field to the specified value only if the specified value is greater than the current value of the field.

If the field doesn't exist, the field is set to the specified value.

Example

class User(
    val name: String,
    val score: Int,
)

collection.updateMany {
    User::score max 100
}

External resources

min

abstract fun <V : Comparable<V>> Field<T, V?>.min(value: V, type: KType)

infix inline fun <V : Comparable<V>> Field<T, V?>.min(value: V)

infix inline fun <V : Comparable<V>> KProperty1<T, V?>.min(value: V)

Updates the value of a field to the specified value only if the specified value is less than the current value of the field.

If the field doesn't exist, the field is set to the specified value.

Example

class User(
    val name: String,
    val score: Int,
)

collection.updateMany {
    User::score min 10
}

External resources

mul

abstract fun <V : Number> Field<T, V>.mul(amount: V, type: KType)

infix inline fun <V : Number> Field<T, V>.mul(amount: V)

infix inline fun <V : Number> KProperty1<T, V>.mul(amount: V)

Multiplies a field by the specified amount.

If the field doesn't exist (either the document doesn't have it, or the operation is an upsert and a new document is created), the field is created with an initial value of 0.

Use of this operator with a field with a null value will generate an error.

Example

class User(
    val name: String,
    val price: Double,
)

collection.updateMany {
    User::price mul 2.0
}

External resources

plusAssign

inline operator fun <V : Number> Field<T, V>.plusAssign(amount: V)

inline operator fun <V : Number> KProperty1<T, V>.plusAssign(amount: V)

Increments a field by the specified amount.

amount may be negative, in which case the field is decremented.

If the field doesn't exist (either the document doesn't have it, or the operation is an upsert and a new document is created), the field is created with an initial value of amount.

Use of this operator with a field with a null value will generate an error.

Example

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

// It's the new year!
collection.updateMany {
    User::age += 1
}

External resources

push

abstract fun <V> Field<T, Collection<V>>.push(value: V, type: KType)

infix inline fun <V> Field<T, Collection<V>>.push(value: V)

infix inline fun <V> KProperty1<T, Collection<V>>.push(value: V)

Adds value at the end of the array.

Unlike addToSet, this operator always adds the value, even if it's already present in the array.

Example

class User(
    val name: String,
    val age: Int,
    val scores: List<Int>,
)

collection.updateOne(
    filter = {
        User::name eq "Bob"
    },
    update = {
        User::scores push 100
    }
)

This will add 100 to the user's scores, even if it's already present.

External resources

abstract fun <V> Field<T, Collection<V>>.push(builder: UpdateQuery.PushBuilder<V>.() -> Unit, type: KType)

infix inline fun <V> Field<T, Collection<V>>.push(noinline builder: UpdateQuery.PushBuilder<V>.() -> Unit)

infix inline fun <V> KProperty1<T, Collection<V>>.push(noinline builder: UpdateQuery.PushBuilder<V>.() -> Unit)

Adds values to the end of the array with advanced options.

This method allows using MongoDB's advanced $push operators like $each and $slice.

Example

class User(
    val name: String,
    val age: Int,
    val tokens: List<String>,
)

collection.updateOne(
    filter = {
        User::name eq "Bob"
    },
    update = {
        User::tokens push {
            each("123", "456")
            slice(3)
        }
    }
)

This will add "123" and "456" to the user's tokens and keep only the last 3 elements.

External resources

pushEach

open fun <V> Field<T, Collection<V>>.pushEach(values: Iterable<V>, type: KType)

infix inline fun <V> Field<T, Collection<V>>.pushEach(values: Iterable<V>)

infix inline fun <V> KProperty1<T, Collection<V>>.pushEach(values: Iterable<V>)

Adds multiple values at the end of the array.

Unlike addToSet, this operator always adds all values, even if they're already present in the array.

This is a convenience function for calling push multiple times.

Example

class User(
    val name: String,
    val age: Int,
    val scores: List<Int>,
)

collection.updateOne(
    filter = {
        User::name eq "Bob"
    },
    update = {
        User::scores pushEach listOf(100, 200)
    }
)

This will add 100 and 200 to the user's scores, even if they're already present.

External resources

renameTo

abstract infix fun <V> Field<T, V>.renameTo(newName: Field<T, V>)

open infix fun <V> KProperty1<T, V>.renameTo(newName: Field<T, V>)

open infix fun <V> Field<T, V>.renameTo(newName: KProperty1<T, V>)

open infix fun <V> KProperty1<T, V>.renameTo(newName: KProperty1<T, V>)

Renames a field.

Example

class User(
    val name: String,
    val age: Int,
    val ageOld: Int,
)

collection.updateMany {
    User::ageOld renameTo User::age
}

External resources

set

abstract fun <V> Field<T, V>.set(value: V, type: KType)

infix inline fun <V> Field<T, V>.set(value: V)

infix inline fun <V> KProperty1<T, V>.set(value: V)

Replaces the value of a field with the specified value.

Example

class User(
    val name: String?,
    val age: Int,
)

collection.filter {
    User::name eq "foo"
}.updateMany {
    User::age set 18
}

External resources

See also

setIf

inline fun <V> Field<T, V>.setIf(condition: Boolean, value: V)

inline fun <V> KProperty1<T, V>.setIf(condition: Boolean, value: V)

Replaces the value of a field with the specified value, if condition is true.

If condition is false, this operator does nothing.

Example

class User(
    val name: String?,
    val age: Int,
)

collection.filter {
    User::name eq "foo"
}.updateMany {
    User::age.setIf(someComplexOperation, 18)
}

External resources

See also

setToCurrentDate

Sets this field to the current date.

If the field does not exist, this operator adds the field to the document.

Example

class User(
    val name: String?,
    val age: Int,
    val modificationDate: Instant,
)

collection.filter {
    User::name eq "Bob"
}.updateMany {
    User::age set 18
    User::modificationDate.setToCurrentDate()
}

External resources

@JvmName(name = "setToCurrentTimestamp")
abstract fun Field<T, Timestamp?>.setToCurrentDate()

@JvmName(name = "setToCurrentTimestamp")
open fun KProperty1<T, Timestamp?>.setToCurrentDate()

Sets this field to the current timestamp.

If the field does not exist, this operator adds the field to the document.

The time Timestamp is internal. Instant should be preferred. For more information, see Timestamp.

Example

class User(
    val name: String?,
    val age: Int,
    val modificationDate: Timestamp,
)

collection.filter {
    User::name eq "Bob"
}.updateMany {
    User::age set 18
    User::modificationDate.setToCurrentDate()
}

External resources

setUnless

inline fun <V> Field<T, V>.setUnless(condition: Boolean, value: V)

inline fun <V> KProperty1<T, V>.setUnless(condition: Boolean, value: V)

Replaces the value of a field with the specified value, if condition is false.

If condition is true, this operator does nothing.

Example

class User(
    val name: String?,
    val age: Int,
)

collection.filter {
    User::name eq "foo"
}.updateMany {
    User::age.setUnless(someComplexOperation, 18)
}

External resources

See also

simplify

@LowLevelApi
abstract fun simplify(): BsonNode?

Returns a simplified (but equivalent) expression to the current expression.

timesAssign

inline operator fun <V : Number> Field<T, V>.timesAssign(amount: V)

inline operator fun <V : Number> KProperty1<T, V>.timesAssign(amount: V)

Multiplies a field by the specified amount.

If the field doesn't exist (either the document doesn't have it, or the operation is an upsert and a new document is created), the field is created with an initial value of 0.

Use of this operator with a field with a null value will generate an error.

Example

class User(
    val name: String,
    val price: Double,
)

collection.updateMany {
    User::price *= 2.0
}

External resources

toBson

Writes the result of simplifying to a new BsonDocument.

toString

abstract override fun toString(): String

JSON representation of this expression.

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.

unset

abstract fun <V> Field<T, V>.unset()

open fun <V> KProperty1<T, V>.unset()

Deletes a field.

Example

class User(
    val name: String,
    val age: Int,
    val alive: Boolean,
)

collection.filter {
    User::name eq "Luke Skywalker"
}.updateOne {
    User::age.unset()
    User::alive set false
}

External resources

writeTo

@LowLevelApi
abstract override fun writeTo(writer: BsonFieldWriter)

Writes the result of simplifying this expression into writer.