Skip to content

MongoDB driver for Kotlin (coroutines)opensavvy.ktmongo.coroutinesJvmMongoCollectionupsertOneWithPipeline

upsertOneWithPipeline

open suspend override fun upsertOneWithPipeline(options: UpdateOptions<Document>.() -> Unit, filter: FilterQuery<Document>.() -> Unit, update: UpdateWithPipelineQuery<Document>.() -> Unit)

Updates a single document that matches filter according to the update pipeline.

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

If no documents match filter, a new one is created.

Example

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

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

External resources

See also