Transaction

classTransaction


ATransaction is passed to a Function to provide the methods to read and write data within the transaction context.

Subclassing Note: Cloud Firestore classes are not meant to be subclassed except for use in test mocks. Subclassing is not supported in production code and new SDK releases may break code that does so.

Summary

Nested types

interfaceTransaction.Function<TResult>

An interface for providing code to be executed within a transaction context.

Public functions

Transaction
delete(documentRef: DocumentReference)

Deletes the document referred to by the providedDocumentReference.

DocumentSnapshot
get(documentRef: DocumentReference)

Reads the document referenced by thisDocumentReference

Transaction
set(documentRef: DocumentReference, data: Any)

Overwrites the document referred to by the providedDocumentReference.

Transaction
set(documentRef: DocumentReference, data: Any, options: SetOptions)

Writes to the document referred to by the provided DocumentReference.

Transaction
update(documentRef: DocumentReference, data: (Mutable)Map<String!, Any!>)

Updates fields in the document referred to by the providedDocumentReference.

Transaction
update(
    documentRef: DocumentReference,
    field: String,
    value: Any?,
    moreFieldsAndValues: Array<Any!>!
)

Updates fields in the document referred to by the providedDocumentReference.

Transaction
update(
    documentRef: DocumentReference,
    fieldPath: FieldPath,
    value: Any?,
    moreFieldsAndValues: Array<Any!>!
)

Updates fields in the document referred to by the providedDocumentReference.

Public functions

delete

fun delete(documentRef: DocumentReference): Transaction

Deletes the document referred to by the providedDocumentReference.

Parameters
documentRef: DocumentReference

TheDocumentReference to delete.

Returns
Transaction

ThisTransaction instance. Used for chaining method calls.

get

fun get(documentRef: DocumentReference): DocumentSnapshot

Reads the document referenced by thisDocumentReference

Parameters
documentRef: DocumentReference

TheDocumentReference to read.

Returns
DocumentSnapshot

The contents of the Document at thisDocumentReference.

Throws
com.google.firebase.firestore.FirebaseFirestoreException: com.google.firebase.firestore.FirebaseFirestoreException

set

fun set(documentRef: DocumentReference, data: Any): Transaction

Overwrites the document referred to by the providedDocumentReference. If the document does not yet exist, it will be created. If a document already exists, it will be overwritten.

Parameters
documentRef: DocumentReference

TheDocumentReference to overwrite.

data: Any

The data to write to the document (like a Map or a POJO containing the desired document contents).

Returns
Transaction

ThisTransaction instance. Used for chaining method calls.

set

fun set(documentRef: DocumentReference, data: Any, options: SetOptions): Transaction

Writes to the document referred to by the provided DocumentReference. If the document does not yet exist, it will be created. If you passSetOptions, the provided data can be merged into an existing document.

Parameters
documentRef: DocumentReference

TheDocumentReference to overwrite.

data: Any

The data to write to the document (like a Map or a POJO containing the desired document contents).

options: SetOptions

An object to configure the set behavior.

Returns
Transaction

ThisTransaction instance. Used for chaining method calls.

update

fun update(documentRef: DocumentReference, data: (Mutable)Map<String!, Any!>): Transaction

Updates fields in the document referred to by the providedDocumentReference. If no document exists yet, the update will fail.

Parameters
documentRef: DocumentReference

TheDocumentReference to update.

data: (Mutable)Map<String!, Any!>

A map of field / value pairs to update. Fields can contain dots to reference nested fields within the document.

Returns
Transaction

ThisTransaction instance. Used for chaining method calls.

update

fun update(
    documentRef: DocumentReference,
    field: String,
    value: Any?,
    moreFieldsAndValues: Array<Any!>!
): Transaction

Updates fields in the document referred to by the providedDocumentReference. If no document exists yet, the update will fail.

Parameters
documentRef: DocumentReference

TheDocumentReference to update.

field: String

The first field to update. Fields can contain dots to reference a nested field within the document.

value: Any?

The first value

moreFieldsAndValues: Array<Any!>!

Additional field/value pairs.

Returns
Transaction

ThisTransaction instance. Used for chaining method calls.

update

fun update(
    documentRef: DocumentReference,
    fieldPath: FieldPath,
    value: Any?,
    moreFieldsAndValues: Array<Any!>!
): Transaction

Updates fields in the document referred to by the providedDocumentReference. If no document exists yet, the update will fail.

Parameters
documentRef: DocumentReference

TheDocumentReference to update.

fieldPath: FieldPath

The first field to update.

value: Any?

The first value

moreFieldsAndValues: Array<Any!>!

Additional field/value pairs.

Returns
Transaction

ThisTransaction instance. Used for chaining method calls.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-21 UTC.