If we try to update a field in a document in Firestore, it throws an error, that it doesn't exist. Should I manually first check the document exist, and if not, create it and then update the field, or there is a better and more elegant practice?
Is there any way to update a specific index from the array in Firestore? No, there is not! This is not possible because if you want to perform an update, you need to know the index of that particular element. When talking about Cloud Firestore arrays, the things are different that you might think.
If either the collection or document does not exist, Cloud Firestore creates it.
You haven't said which language you're using to write your code, but each SDK should have an option to pass to set()
that lets you update a document that already exists. For example, in JavaScript on web clients:
doucmentReference.set({ a: 1 }, { merge: true })
That merge: true
will update the data if the document already exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With