kmongo / org.litote.kmongo / com.mongodb.client.MongoCollection / replaceOneWithFilter

replaceOneWithFilter

fun <T : Any> MongoCollection<T>.replaceOneWithFilter(filter: Bson, replacement: T, replaceOptions: ReplaceOptions = ReplaceOptions()): UpdateResult (source)

Replace a document in the collection according to the specified arguments. Same than MongoCollection.replaceOne but ensure that any _id present in replacement is removed to avoid MongoWriteException such as: "After applying the update, the (immutable) field '_id' was found to have been altered to _id"

Note: Supports retryable writes on MongoDB server versions 3.6 or higher when the retryWrites setting is enabled.

Parameters

filter - the query filter to apply the the replace operation

replacement - the replacement document

replaceOptions - the options to apply to the replace operation

Exceptions

com.mongodb.MongoWriteException - if the write failed due some other failure specific to the replace command

com.mongodb.MongoWriteConcernException - if the write failed due being unable to fulfil the write concern

com.mongodb.MongoException - if the write failed due some other failure

Return
the result of the replace one operation

Since
3.7

fun <T : Any> MongoCollection<T>.replaceOneWithFilter(clientSession: ClientSession, filter: Bson, replacement: T, replaceOptions: ReplaceOptions = ReplaceOptions()): UpdateResult (source)

Replace a document in the collection according to the specified arguments. Same than MongoCollection.replaceOne but ensure that any _id present in replacement is removed to avoid MongoWriteException such as: "After applying the update, the (immutable) field '_id' was found to have been altered to _id"

Note: Supports retryable writes on MongoDB server versions 3.6 or higher when the retryWrites setting is enabled.

Parameters

clientSession - the client session with which to associate this operation

filter - the query filter to apply the the replace operation

replacement - the replacement document

replaceOptions - the options to apply to the replace operation

Exceptions

com.mongodb.MongoWriteException - if the write failed due some other failure specific to the replace command

com.mongodb.MongoWriteConcernException - if the write failed due being unable to fulfil the write concern

com.mongodb.MongoException - if the write failed due some other failure

Return
the result of the replace one operation

Since
3.7