Transaction

public 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

public interfaceTransaction.Function<TResult>

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

Public methods

@NonNullTransaction

Deletes the document referred to by the providedDocumentReference.

@NonNullDocumentSnapshot

Reads the document referenced by thisDocumentReference

@NonNullTransaction

Overwrites the document referred to by the providedDocumentReference.

@NonNullTransaction
set(
    @NonNullDocumentReference documentRef,
    @NonNullObject data,
    @NonNullSetOptions options
)

Writes to the document referred to by the provided DocumentReference.

@NonNullTransaction
update(
    @NonNullDocumentReference documentRef,
    @NonNullMap<StringObject> data
)

Updates fields in the document referred to by the providedDocumentReference.

@NonNullTransaction
update(
    @NonNullDocumentReference documentRef,
    @NonNullString field,
    @NullableObject value,
    Object[] moreFieldsAndValues
)

Updates fields in the document referred to by the providedDocumentReference.

@NonNullTransaction
update(
    @NonNullDocumentReference documentRef,
    @NonNullFieldPath fieldPath,
    @NullableObject value,
    Object[] moreFieldsAndValues
)

Updates fields in the document referred to by the providedDocumentReference.

Public methods

delete

public @NonNullTransaction delete(@NonNullDocumentReference documentRef)

Deletes the document referred to by the providedDocumentReference.

Parameters
@NonNullDocumentReference documentRef

TheDocumentReference to delete.

Returns
@NonNullTransaction

ThisTransaction instance. Used for chaining method calls.

get

public @NonNullDocumentSnapshot get(@NonNullDocumentReference documentRef)

Reads the document referenced by thisDocumentReference

Parameters
@NonNullDocumentReference documentRef

TheDocumentReference to read.

Returns
@NonNullDocumentSnapshot

The contents of the Document at thisDocumentReference.

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

set

public @NonNullTransaction set(@NonNullDocumentReference documentRef, @NonNullObject data)

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
@NonNullDocumentReference documentRef

TheDocumentReference to overwrite.

@NonNullObject data

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

Returns
@NonNullTransaction

ThisTransaction instance. Used for chaining method calls.

set

public @NonNullTransaction set(
    @NonNullDocumentReference documentRef,
    @NonNullObject data,
    @NonNullSetOptions options
)

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
@NonNullDocumentReference documentRef

TheDocumentReference to overwrite.

@NonNullObject data

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

@NonNullSetOptions options

An object to configure the set behavior.

Returns
@NonNullTransaction

ThisTransaction instance. Used for chaining method calls.

update

public @NonNullTransaction update(
    @NonNullDocumentReference documentRef,
    @NonNullMap<StringObject> data
)

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

Parameters
@NonNullDocumentReference documentRef

TheDocumentReference to update.

@NonNullMap<StringObject> data

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

Returns
@NonNullTransaction

ThisTransaction instance. Used for chaining method calls.

update

public @NonNullTransaction update(
    @NonNullDocumentReference documentRef,
    @NonNullString field,
    @NullableObject value,
    Object[] moreFieldsAndValues
)

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

Parameters
@NonNullDocumentReference documentRef

TheDocumentReference to update.

@NonNullString field

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

@NullableObject value

The first value

Object[] moreFieldsAndValues

Additional field/value pairs.

Returns
@NonNullTransaction

ThisTransaction instance. Used for chaining method calls.

update

public @NonNullTransaction update(
    @NonNullDocumentReference documentRef,
    @NonNullFieldPath fieldPath,
    @NullableObject value,
    Object[] moreFieldsAndValues
)

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

Parameters
@NonNullDocumentReference documentRef

TheDocumentReference to update.

@NonNullFieldPath fieldPath

The first field to update.

@NullableObject value

The first value

Object[] moreFieldsAndValues

Additional field/value pairs.

Returns
@NonNullTransaction

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.