findOneAndReplace

fun findOneAndReplace(filter: Bson, replacement: T, options: FindOneAndReplaceOptions = FindOneAndReplaceOptions()): T?

Atomically find a document and replace it.

Use this method to replace a document using the specified replacement argument. To update the document with update operators, use the corresponding findOneAndUpdate method.

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

Return

the document that was replaced. Depending on the value of the returnOriginal property, this will either be the document as it was before the update or as it is after the update. If no documents matched the query filter, then null will be returned

Parameters

filter

the query filter to apply the replace operation

replacement

the replacement document

options

the options to apply to the operation

See also


fun findOneAndReplace(clientSession: ClientSession, filter: Bson, replacement: T, options: FindOneAndReplaceOptions = FindOneAndReplaceOptions()): T?

Atomically find a document and replace it.

Use this method to replace a document using the specified replacement argument. To update the document with update operators, use the corresponding findOneAndUpdate method.

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

Return

the document that was replaced. Depending on the value of the returnOriginal property, this will either be the document as it was before the update or as it is after the update. If no documents matched the query filter, then null will be returned

Parameters

clientSession

the client session with which to associate this operation

filter

the query filter to apply the replace operation

replacement

the replacement document

options

the options to apply to the operation

See also