repsertOne

fun repsertOne(options: ReplaceOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, document: Document)(source)

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

Replace an existing document.

Always create a new document.