Cloud Firestore stores data in Documents, which are stored in Collections. Cloud Firestore creates collections and documents implicitly the first time you add data to the document. You do not need to explicitly create collections or documents.
There are several ways to write data to Cloud Firestore:
- Set the data of a document within a collection, explicitly specifying a document identifier.
- Add a new document to a collection. In this case, Cloud Firestore automatically generates the document identifier.
- Create an empty document with an automatically generated identifier, and assign data to it later.
This guide explains how to use the
update() methods to write data to individual documents in Cloud Firestore.
To create or overwrite a single document, use the
setDocument() call will dispatch one of two events:
DocumentReferenceEvent.SET_SUCCESS: The document has been correctly set with the new data
DocumentReferenceEvent.SET_ERROR: There was an error setting the data to the document
You should ensure you remove the event listeners at this point to ensure the extension can correctly clean up memory usage of unused document references.
Alternatively you can add a success and failure listener directly:
If the document does not exist, it will be created. If the document does exist, its contents will be overwritten with the newly provided data, unless you specify that the data should be merged into the existing document, as follows:
If you're not sure whether the document exists, pass the option to merge the new data with any existing document to avoid overwriting entire documents.
Cloud Firestore lets you write a variety of data types inside a document, including strings, booleans, numbers, dates, null, and nested arrays and objects. Cloud Firestore always stores numbers as doubles, regardless of what type of number you use in your code.
You can use a
com.distriqt.extension.firebase.firestore.Blob) to add
ByteArray style data to your documents.
Blob data type inherits from a
ByteArray and handles conversion to and from the Firestore Blob format. So you can simply use the standard
ByteArray functions to store data and then add it to your document.
For example, construct a
Blob and write 2 bytes to it:
Then use this as a value in your data object and pass it to
Not currently supported
When you use
setDocument() to create a document, you must specify an ID for the document to create. For example:
But sometimes there isn't a meaningful ID for the document, and it's more convenient to let Cloud Firestore auto-generate an ID for you. You can do this by calling
In some cases, it can be useful to create a document reference with an auto-generated ID, then use the reference later. For this use case, you can call
Behind the scenes,
.document().setDocument(...) are completely equivalent, so you can use whichever is more convenient.
To update some fields of a document without overwriting the entire document, use the
You can also add server timestamps to specific fields in your documents, to track when an update was received by the server: