英文:
Firestore batch - actions which requires more than 1 write
问题
TL;DR - 我们在使用Firestore的批量写入操作时,发现当将一个使用Firestore的serverTimestamp
特性的对象添加到批处理中时,会产生2次写入操作而不是1次。目前没有找到相关的文档说明。问题在于,如果在代码中使用本地计数器来包装Firestore批处理,以避免达到500的限制,可能会错误计算批处理大小。
我的问题是 - 是否还有其他特殊的Firestore值/操作等需要进行多次批处理写入操作的情况?还有谁能提供解决这个问题的方法?
英文:
TL;DR -
There are conventions/payloads/actions out there, which requires more than 1 batch write to take place.
It occur to me that when im adding a set
operation to a batch
, for an object which is using firestore serverTimstamp
feature, it costs 2 write operations rather than 1.
Could not find any documentation about that.
The problem is, that if you wrap firestore batch with a local counter in your code, to avoid reaching the 500 threshold, you might miss-calculate batch size.
In my example, I am using Golang firestore sdk (but I do believe this issue will be common for all other sdk's because it derives from firestore fundamental mechanism)
batch.Set(ctx, docRef, struct {
LastUpdate time.Time `firestore:"lastUpdate,serverTimestamp"`
}{}, firestore.MergeAll)
--> batch.writes was increased by 2 writes rather than 1
My question is - are any other special firestore values/actions etc' which might also require more than 1 batch write operation to take place?
Also do anyone can suggest a workaround for that issue?
答案1
得分: 2
所谓的字段转换操作确实可能需要在服务器上进行多次写操作,从而降低了您在批量写入或事务中执行的逻辑写入操作的能力。如果我记得正确的话,在这种情况下,您只会被收费一次写操作。
根据我之前在https://stackoverflow.com/questions/70764076/what-is-field-transformations-in-firestore 上给出的答案,可用的字段转换操作包括setToServerValue
(您使用的服务器时间戳)、increment
、appendMissingElements
、removeAllFromArray
、maximum
和minimum
。我不确定最后两个是否已在任何SDK中公开。
英文:
So-called field transform operations indeed may require more than one write operation on the server, reducing the capacity of logical write operations you can perform in a batched write or transaction. If I recall correct, you only be charged for one write operation in such cases though.
From the answer I gave before to https://stackoverflow.com/questions/70764076/what-is-field-transformations-in-firestore, it seems that the field transform operations that are available are setToServerValue
(the server timestamp you use), increment
, appendMissingElements
, removeAllFromArray
, maximum
and minimum
. I'm not sure if the last two are exposed on any SDK yet.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论