findOneAndUpdate

fun findOneAndUpdate(filter: Bson, update: Bson, options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()): T?

Atomically find a document and update it.

Use this method to only update the corresponding fields in the document according to the update operators used in the update document. To replace the entire document with a new document, use the corresponding findOneAndReplace method.

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

Return

the document that was updated. 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

a document describing the query filter, which may not be null.

update

a document describing the update, which may not be null. The update to apply must include at least one update operator.

options

the options to apply to the operation

See also

MongoCollection.findOneAndReplace

fun findOneAndUpdate(clientSession: ClientSession, filter: Bson, update: Bson, options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()): T?

Atomically find a document and update it.

Use this method to only update the corresponding fields in the document according to the update operators used in the update document. To replace the entire document with a new document, use the corresponding findOneAndReplace method.

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

Return

the document that was updated. 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

a document describing the query filter, which may not be null.

update

a document describing the update, which may not be null. The update to apply must include at least one update operator.

options

the options to apply to the operation

See also

MongoCollection.findOneAndReplace

fun findOneAndUpdate(filter: Bson, update: List<Bson>, options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()): T?

Atomically find a document and update it.

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

Return

the document that was updated. 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

a document describing the query filter, which may not be null.

update

a pipeline describing the update, which may not be null.

options

the options to apply to the operation


fun findOneAndUpdate(clientSession: ClientSession, filter: Bson, update: List<Bson>, options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()): T?

Atomically find a document and update it.

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

Return

the document that was updated. 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

a document describing the query filter, which may not be null.

update

a pipeline describing the update, which may not be null.

options

the options to apply to the operation