WriteBatch class Stay organized with collections Save and categorize content based on your preferences.
A write batch, used to perform multiple writes as a single atomic unit.
AWriteBatch object can be acquired by callingwriteBatch(). It provides methods for adding writes to the write batch. None of the writes will be committed (or visible locally) untilWriteBatch.commit() is called.
Signature:
exportdeclareclassWriteBatchMethods
| Method | Modifiers | Description |
|---|---|---|
| commit() | Commits all of the writes in this write batch as a single atomic unit.The result of these writes will only be reflected in document reads that occur after the returned promise resolves. If the client is offline, the write fails. If you would like to see local modifications or buffer writes until the client is online, use the full Firestore SDK. | |
| delete(documentRef) | Deletes the document referred to by the providedDocumentReference. | |
| set(documentRef, data) | Writes to the document referred to by the providedDocumentReference. If the document does not exist yet, it will be created. | |
| set(documentRef, data, options) | Writes to the document referred to by the providedDocumentReference. If the document does not exist yet, it will be created. If you providemerge ormergeFields, the provided data can be merged into an existing document. | |
| update(documentRef, data) | Updates fields in the document referred to by the providedDocumentReference. The update will fail if applied to a document that does not exist. | |
| update(documentRef, field, value, moreFieldsAndValues) | 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 strings or by providingFieldPath objects. |
WriteBatch.commit()
Commits all of the writes in this write batch as a single atomic unit.
The result of these writes will only be reflected in document reads that occur after the returned promise resolves. If the client is offline, the write fails. If you would like to see local modifications or buffer writes until the client is online, use the full Firestore SDK.
Signature:
commit():Promise<void>;Returns:
Promise<void>
APromise resolved once all of the writes in the batch have been successfully written to the backend as an atomic unit (note that it won't resolve while you're offline).
WriteBatch.delete()
Deletes the document referred to by the providedDocumentReference.
Signature:
delete<AppModelType,DbModelTypeextendsDocumentData>(documentRef:DocumentReference<AppModelType,DbModelType>):WriteBatch;Parameters
| Parameter | Type | Description |
|---|---|---|
| documentRef | DocumentReference<AppModelType, DbModelType> | A reference to the document to be deleted. |
Returns:
ThisWriteBatch instance. Used for chaining method calls.
WriteBatch.set()
Writes to the document referred to by the providedDocumentReference. If the document does not exist yet, it will be created.
Signature:
set<AppModelType,DbModelTypeextendsDocumentData>(documentRef:DocumentReference<AppModelType,DbModelType>,data:WithFieldValue<AppModelType>):WriteBatch;Parameters
| Parameter | Type | Description |
|---|---|---|
| documentRef | DocumentReference<AppModelType, DbModelType> | A reference to the document to be set. |
| data | WithFieldValue<AppModelType> | An object of the fields and values for the document. |
Returns:
ThisWriteBatch instance. Used for chaining method calls.
WriteBatch.set()
Writes to the document referred to by the providedDocumentReference. If the document does not exist yet, it will be created. If you providemerge ormergeFields, the provided data can be merged into an existing document.
Signature:
set<AppModelType,DbModelTypeextendsDocumentData>(documentRef:DocumentReference<AppModelType,DbModelType>,data:PartialWithFieldValue<AppModelType>,options:SetOptions):WriteBatch;Parameters
| Parameter | Type | Description |
|---|---|---|
| documentRef | DocumentReference<AppModelType, DbModelType> | A reference to the document to be set. |
| data | PartialWithFieldValue<AppModelType> | An object of the fields and values for the document. |
| options | SetOptions | An object to configure the set behavior. |
Returns:
ThisWriteBatch instance. Used for chaining method calls.
Exceptions
Error - If the provided input is not a valid Firestore document.
WriteBatch.update()
Updates fields in the document referred to by the providedDocumentReference. The update will fail if applied to a document that does not exist.
Signature:
update<AppModelType,DbModelTypeextendsDocumentData>(documentRef:DocumentReference<AppModelType,DbModelType>,data:UpdateData<DbModelType>):WriteBatch;Parameters
| Parameter | Type | Description |
|---|---|---|
| documentRef | DocumentReference<AppModelType, DbModelType> | A reference to the document to be updated. |
| data | UpdateData<DbModelType> | An object containing the fields and values with which to update the document. Fields can contain dots to reference nested fields within the document. |
Returns:
ThisWriteBatch instance. Used for chaining method calls.
Exceptions
Error - If the provided input is not valid Firestore data.
WriteBatch.update()
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 strings or by providingFieldPath objects.
Signature:
update<AppModelType,DbModelTypeextendsDocumentData>(documentRef:DocumentReference<AppModelType,DbModelType>,field:string|FieldPath,value:unknown,...moreFieldsAndValues:unknown[]):WriteBatch;Parameters
| Parameter | Type | Description |
|---|---|---|
| documentRef | DocumentReference<AppModelType, DbModelType> | A reference to the document to be updated. |
| field | string |FieldPath | The first field to update. |
| value | unknown | The first value. |
| moreFieldsAndValues | unknown[] | Additional key value pairs. |
Returns:
ThisWriteBatch instance. Used for chaining method calls.
Exceptions
Error - If the provided input is not valid Firestore data.
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 2024-01-19 UTC.