Skip to content

MongoDB driver for Kotlin (synchronous)opensavvy.ktmongo.syncJvmMongoCollectionupdateOne

updateOne

open override fun updateOne(options: UpdateOptions<Document>.() -> Unit, filter: FilterQuery<Document>.() -> Unit, update: UpdateQuery<Document>.() -> Unit)

Updates a single document that matches filter according to update.

If multiple documents match filter, only the first one found is updated.

Example

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

collection.updateOne(
    filter = {
        User::name eq "Patrick"
    },
    update = {
        User::age set 15
    },
)

Using filtered collections

The following code is equivalent:

collection.filter {
    User::name eq "Patrick"
}.updateOne {
    User::age set 15
}

To learn more, see filter.

External resources

Parameters

filter

Optional filter to select which document is updated. If no filter is specified, the first document found is updated.

See also