MongoDB request DSL • opensavvy.ktmongo.dsl.command • BulkWrite • upsertOne
upsertOne¶
fun upsertOne(options: UpdateOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, update: UpsertQuery<Document>.() -> Unit)
Updates a single document that matches filter
according to update
.
If multiple documents match filter
, only the first one found is updated.
If no documents match filter
, a new one is created.
Example¶
class User(
val name: String,
val age: Int,
)
collection.bulkWrite {
upsertOne(
filter = { User::name eq "Patrick" },
update = {
User::age set 15
}
)
}
If a document exists that has the name
of "Patrick", its age is set to 15. If none exist, a document with name
"Patrick" and age
15 is created.
External resources¶
See also¶
-
BulkWrite.insertOne
Always create a new document. -
BulkWrite.updateOne
Do nothing if no matching documents are found.