英文:
Writing to Firestore exactly the same data object as in database's document
问题
"使用以下代码设置已存在于Firestore数据库文档中的相同数据是否算作写操作,还是针对此用例进行了优化?
await setDoc(refDoc, { the_same_data, as_in_refDoc })
"
英文:
Does setting the same data that already exists in a Firestore db document with the following code counts as a write operation or is it optimised for this use case?
await setDoc(refDoc, { the_same_data, as_in_refDoc })
答案1
得分: 0
setDoc()
无论现有文档是否存在,也无论现有文档的内容如何,都会被计为写操作。在写入文档之前,它不会读取文档以确保不会多余地写入数据;它覆盖了该位置的任何现有文档,而不考虑其内容。
这在了解 Cloud Firestore 计费文档中有说明:
> 您会为每个在 Cloud Firestore 上执行的文档读取、写入和删除操作收费。
>
> 写入和删除的收费非常直接。对于写入操作,每个set
或update
操作都算作一次写入。
这一点的明显指示是,即使使用多余的数据调用set()
,仍然会更新文档上的元数据,通常您看不到,比如时间戳数据。即使您没有更改文档中的任何字段,数据仍然会被写入。
英文:
setDoc()
counts as a write regardless of the existence of any existing document and regardless of the contents of any existing document. It does not read the document before writing it to ensure that it does not redundantly write data; it overwrites any existing document at that location regardless of its contents.
This is documented in Understand Cloud Firestore billing:
> You are charged for each document read, write, and delete that you perform with Cloud Firestore.
>
> Charges for writes and deletes are straightforward. For writes, each set
or update
operation counts as a single write.
The obvious indicator for this is that calling set()
with redundant data still updates metadata on the document that you don't normally see, like timestamp data. Data is still written even if you haven't changed any fields in the document.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论