MongoDB request DSL • opensavvy.ktmongo.dsl.command • BulkWrite • repsertOne
repsertOne¶
fun repsertOne(options: ReplaceOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, document: Document)
Replaces a document that matches filter by document.
If multiple documents match filter, only the first one found is updated.
If no documents match filter, document is inserted.
Example¶
class User(
val name: String,
val age: Int,
)
collection.bulkWrite {
repsertOne(
filter = { User::name eq "Patrick" },
document = User("Bob", 15)
)
}
If a document exists that has the name of "Patrick", it is replaced by the new document. If none exist, the document is inserted.
External resources¶
See also¶
-
BulkWrite.replaceOneReplace an existing document. -
BulkWrite.insertOneAlways create a new document.