Skip to content

MongoDB request DSLopensavvy.ktmongo.dsl.queryUpsertQuerysetOnInsert

setOnInsert

abstract infix fun <V> Field<T, V>.setOnInsert(value: V)

If an upsert operation results in an insert of a document, then this operator assigns the specified value to the field. If the update operation does not result in an insert, this operator does nothing.

Example

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

collection.filter {
    User::name eq "foo"
}.upsertOne {
    User::age setOnInsert 18
}

External resources

See also

  • UpsertQuery.set Always set the value.
open infix fun <V> KProperty1<T, V>.setOnInsert(value: V)

If an upsert operation results in an insert of a document, then this operator assigns the specified value to the field. If the update operation does not result in an insert, this operator does nothing.

Example

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

collection.filter {
    User::name eq "foo"
}.upsertOne {
    User::age setOnInsert 18
}

External resources

See also

  • UpsertQuery.set Always set the value.