A write batch, used to perform multiple writes as a single atomic unit.

AWriteBatch object can be acquired by callingFirestore.batch(). Itprovides methods for adding writes to the write batch. None of thewrites will be committed (or visible locally) untilWriteBatch.commit()is called.

Unlike transactions, write batches are persisted offline and therefore arepreferable when you don't need to condition your writes on read data.

Index

Constructors

Private constructor

Methods

commit

  • commit():Promise<void>
  • Commits all of the writes in this write batch as a single atomic unit.

    ReturnsPromise<void>

    A Promise resolved once all of the writes in the batch have beensuccessfully written to the backend as an atomic unit. Note that it won'tresolve while you're offline.

delete

  • delete(documentRefDocumentReference<any>):WriteBatch
  • Deletes the document referred to by the providedDocumentReference.

    Parameters

    ReturnsWriteBatch

    ThisWriteBatch instance. Used for chaining method calls.

set

  • set<T>(documentRefDocumentReference<T>, dataPartial<T>, optionsSetOptions):WriteBatch
  • Writes to the document referred to by the providedDocumentReference.If the document does not exist yet, it will be created. If you passSetOptions, the provided data can be merged into the existing document.

    Type parameters

    • T

    Parameters

    • documentRef:DocumentReference<T>

      A reference to the document to be set.

    • data:Partial<T>

      An object of the fields and values for the document.

    • options:SetOptions

      An object to configure the set behavior.

    ReturnsWriteBatch

    ThisWriteBatch instance. Used for chaining method calls.

  • set<T>(documentRefDocumentReference<T>, dataT):WriteBatch
  • Writes to the document referred to by the providedDocumentReference.If the document does not exist yet, it will be created. If you passSetOptions, the provided data can be merged into the existing document.

    Type parameters

    • T

    Parameters

    • documentRef:DocumentReference<T>

      A reference to the document to be set.

    • data:T

      An object of the fields and values for the document.

    ReturnsWriteBatch

    ThisWriteBatch instance. Used for chaining method calls.

update

  • update(documentRefDocumentReference<any>, dataUpdateData):WriteBatch
  • Updates fields in the document referred to by the providedDocumentReference. The update will fail if applied to a document thatdoes not exist.

    Parameters

    • documentRef:DocumentReference<any>

      A reference to the document to be updated.

    • data:UpdateData

      An object containing the fields and values with which toupdate the document. Fields can contain dots to reference nested fieldswithin the document.

    ReturnsWriteBatch

    ThisWriteBatch instance. Used for chaining method calls.

  • update(documentRefDocumentReference<any>, fieldstring |FieldPath, valueany...moreFieldsAndValuesany[]):WriteBatch
  • Updates fields in the document referred to by thisDocumentReference.The update will fail if applied to a document that does not exist.

    Nested fields can be update by providing dot-separated field path stringsor by providing FieldPath objects.

    Parameters

    • documentRef:DocumentReference<any>

      A reference to the document to be updated.

    • field:string |FieldPath

      The first field to update.

    • value:any

      The first value.

    • Rest...moreFieldsAndValues:any[]

      Additional key value pairs.

    ReturnsWriteBatch

    A Promise resolved once the data has been successfully writtento the backend (Note that it won't resolve while you're offline).

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 2022-07-27 UTC.