There are a few ways to write data to Cloud Firestore:
- update() - change fields on a document that already exists. Fails if the document does not exist.
- set() - overwrite the data in a document, creating the document if it does not already exist. If you want to only partially update a document, use
SetOptions.merge().
- create() - only available in the server-side SDKs, similar to
set() but fails if the document already exists.
So you just need to use the correct operation for your use case. If you want to update a document without knowing if it exists or not, you want to use a set() with the merge() option like this (this example is for Java):
// Update one field, creating the document if it does not already exist.
Map<String, Object> data = new HashMap<>();
data.put("capital", true);
db.collection("cities").document("BJ")
.set(data, SetOptions.merge());