1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Firestore WriteBatch.commit() で特定の FieldValue を含むデータと件数制限について

Last updated at Posted at 2020-07-22

500レコード以下でも件数制限に引っかかる時がある。

Firebase Firestore のトランザクション書き込みには最大500レコードまでしか同時に書き込めません (執筆時時点。

500レコードをこえるデータを書き込もうとすると、

maximum 500 writes allowed per request

というエラーが返されます。

一方 500 レコード以下の書き込みでも、上記のエラーになることがあります。
serverTimestamparrayUnionincrement などのフィールド変換のデータを含むときです。

理由

ドキュメントに書いてある
https://firebase.google.com/docs/firestore/manage-data/transactions?hl=ja#batched-writes

つまり、serverTimestamparrayUnionincrement などのフィールド変換は 1件 として計上されるので、
たとえば、

{
 updatedAt:  admin.firestore.FieldValue.serverTimestamp()
 title: 'foor'
}

みたいなデータの書き込みは 2レコードの書き込みとして計上されてしまいます。
同様のデータを251件書き込もうとするとエラーになってしまうわけです。

まとめ

ドキュメントにもあるので、知らなかったお前が悪いというのはそのとおりなんですが、、、、
壮大にハマったので誰かのためになればいいなと思い書いときます。

https://github.com/firebase/firebase-admin-node/issues/456#issuecomment-464187898
にもあるとおり、中の人も 「ごめん、むりだ。その代わりドキュメントに書いとくYO」 といっているので、バグではなく仕様ですね。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?