Stay organized with collections
Save and categorize content based on your preferences.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-07-27 UTC.
[null,null,["Last updated 2022-07-27 UTC."],[],[],null,["- [firebase](/docs/reference/js/v8/firebase).\n- [firestore](/docs/reference/js/v8/firebase.firestore).\n- WriteBatch \nA write batch, used to perform multiple writes as a single atomic unit.\n\nA `WriteBatch` object can be acquired by calling `Firestore.batch()`. It\nprovides methods for adding writes to the write batch. None of the\nwrites will be committed (or visible locally) until `WriteBatch.commit()`\nis called.\n\nUnlike transactions, write batches are persisted offline and therefore are\npreferable when you don't need to condition your writes on read data.\n\nIndex\n\nConstructors\n\n- [constructor](/docs/reference/js/v8/firebase.firestore.WriteBatch#constructor)\n\nMethods\n\n- [commit](/docs/reference/js/v8/firebase.firestore.WriteBatch#commit)\n- [delete](/docs/reference/js/v8/firebase.firestore.WriteBatch#delete)\n- [set](/docs/reference/js/v8/firebase.firestore.WriteBatch#set)\n- [update](/docs/reference/js/v8/firebase.firestore.WriteBatch#update)\n\nConstructors\n\nPrivate constructor\n\n- new WriteBatch ( ) : [WriteBatch](/docs/reference/js/v8/firebase.firestore.WriteBatch)\n-\n\n Returns [WriteBatch](/docs/reference/js/v8/firebase.firestore.WriteBatch)\n\nMethods\n\ncommit\n\n- commit ( ) : Promise \\\u003c void \\\u003e\n- Commits all of the writes in this write batch as a single atomic unit.\n\n Returns Promise\\\u003cvoid\\\u003e\n\n A Promise resolved once all of the writes in the batch have been\n successfully written to the backend as an atomic unit. Note that it won't\n resolve while you're offline.\n\ndelete\n\n- delete ( documentRef : [DocumentReference](/docs/reference/js/v8/firebase.firestore.DocumentReference) \\\u003c any \\\u003e ) : [WriteBatch](/docs/reference/js/v8/firebase.firestore.WriteBatch)\n- Deletes the document referred to by the provided `DocumentReference`.\n\n Parameters\n -\n\n documentRef: [DocumentReference](/docs/reference/js/v8/firebase.firestore.DocumentReference)\\\u003cany\\\u003e \n A reference to the document to be deleted.\n\n Returns [WriteBatch](/docs/reference/js/v8/firebase.firestore.WriteBatch)\n\n This `WriteBatch` instance. Used for chaining method calls.\n\nset\n\n- set \\\u003c T \\\u003e ( documentRef : [DocumentReference](/docs/reference/js/v8/firebase.firestore.DocumentReference) \\\u003c T \\\u003e , data : Partial \\\u003c T \\\u003e , options : [SetOptions](/docs/reference/js/v8/firebase.firestore.SetOptions) ) : [WriteBatch](/docs/reference/js/v8/firebase.firestore.WriteBatch)\n- Writes to the document referred to by the provided `DocumentReference`.\n If the document does not exist yet, it will be created. If you pass\n `SetOptions`, the provided data can be merged into the existing document.\n\n Type parameters\n -\n\n T\n\n Parameters\n -\n\n documentRef: [DocumentReference](/docs/reference/js/v8/firebase.firestore.DocumentReference)\\\u003cT\\\u003e \n A reference to the document to be set.\n -\n\n data: Partial\\\u003cT\\\u003e \n An object of the fields and values for the document.\n -\n\n options: [SetOptions](/docs/reference/js/v8/firebase.firestore.SetOptions) \n An object to configure the set behavior.\n\n Returns [WriteBatch](/docs/reference/js/v8/firebase.firestore.WriteBatch)\n\n This `WriteBatch` instance. Used for chaining method calls.\n- set \\\u003c T \\\u003e ( documentRef : [DocumentReference](/docs/reference/js/v8/firebase.firestore.DocumentReference) \\\u003c T \\\u003e , data : T ) : [WriteBatch](/docs/reference/js/v8/firebase.firestore.WriteBatch)\n- Writes to the document referred to by the provided `DocumentReference`.\n If the document does not exist yet, it will be created. If you pass\n `SetOptions`, the provided data can be merged into the existing document.\n\n Type parameters\n -\n\n T\n\n Parameters\n -\n\n documentRef: [DocumentReference](/docs/reference/js/v8/firebase.firestore.DocumentReference)\\\u003cT\\\u003e \n A reference to the document to be set.\n -\n\n data: T \n An object of the fields and values for the document.\n\n Returns [WriteBatch](/docs/reference/js/v8/firebase.firestore.WriteBatch)\n\n This `WriteBatch` instance. Used for chaining method calls.\n\nupdate\n\n- update ( documentRef : [DocumentReference](/docs/reference/js/v8/firebase.firestore.DocumentReference) \\\u003c any \\\u003e , data : [UpdateData](/docs/reference/js/v8/firebase.firestore#updatedata) ) : [WriteBatch](/docs/reference/js/v8/firebase.firestore.WriteBatch)\n- Updates fields in the document referred to by the provided\n `DocumentReference`. The update will fail if applied to a document that\n does not exist.\n\n Parameters\n -\n\n documentRef: [DocumentReference](/docs/reference/js/v8/firebase.firestore.DocumentReference)\\\u003cany\\\u003e \n A reference to the document to be updated.\n -\n\n data: [UpdateData](/docs/reference/js/v8/firebase.firestore#updatedata) \n An object containing the fields and values with which to\n update the document. Fields can contain dots to reference nested fields\n within the document.\n\n Returns [WriteBatch](/docs/reference/js/v8/firebase.firestore.WriteBatch)\n\n This `WriteBatch` instance. Used for chaining method calls.\n- update ( documentRef : [DocumentReference](/docs/reference/js/v8/firebase.firestore.DocumentReference) \\\u003c any \\\u003e , field : string \\| [FieldPath](/docs/reference/js/v8/firebase.firestore.FieldPath) , value : any , ... moreFieldsAndValues : any \\[\\] ) : [WriteBatch](/docs/reference/js/v8/firebase.firestore.WriteBatch)\n- Updates fields in the document referred to by this `DocumentReference`.\n The update will fail if applied to a document that does not exist.\n\n Nested fields can be update by providing dot-separated field path strings\n or by providing FieldPath objects.\n\n Parameters\n -\n\n documentRef: [DocumentReference](/docs/reference/js/v8/firebase.firestore.DocumentReference)\\\u003cany\\\u003e \n A reference to the document to be updated.\n -\n\n field: string \\| [FieldPath](/docs/reference/js/v8/firebase.firestore.FieldPath) \n The first field to update.\n -\n\n value: any \n The first value.\n -\n\n Rest ...moreFieldsAndValues: any\\[\\] \n Additional key value pairs.\n\n Returns [WriteBatch](/docs/reference/js/v8/firebase.firestore.WriteBatch)\n\n A Promise resolved once the data has been successfully written\nto the backend (Note that it won't resolve while you're offline)."]]
A write batch, used to perform multiple writes as a single atomic unit.
A
WriteBatch
object can be acquired by callingFirestore.batch()
. It provides methods for adding writes to the write batch. None of the writes will be committed (or visible locally) untilWriteBatch.commit()
is called.Unlike transactions, write batches are persisted offline and therefore are preferable when you don't need to condition your writes on read data.