在 Google Cloud Firestore 中使用 UUID 作为文档 ID?

Using UUID as document ID in Google Cloud Firestore?

我们开始将 Google Cloud Firestore 与 Spring Cloud GCP 结合使用。

我们正在考虑为每个文档添加一个 UUID 作为文档 ID -- 主要是因为我们已经习惯了使用 SQL 数据库。

但是,Firestore 中似乎没有对 UUID 的原生支持。在 Firestore 中使用 UUID 作为文档 ID 有什么优点或缺点吗?

使用 UUID 作为 Firestore 文档 ID 应该没有任何问题。

Firestore文档ID的约束条件如下,详见doc

Constraints on document IDs

  • Must be valid UTF-8 characters
  • Must be no longer than 1,500 bytes
  • Cannot contain a forward slash (/)
  • Cannot solely consist of a single period (.) or double periods (..)
  • Cannot match the regular expression .*

还有一组Best Practices,如下,不过,AFAK应该不是UUID的问题:

  • Avoid the document IDs . and ...
  • Avoid using / forward slashes in document IDs.

  • Do not use monotonically increasing document IDs such as:

    • Customer1, Customer2, Customer3, ...
    • Product 1, Product 2, Product 3, ...

      Such sequential IDs can lead to hotspots that impact latency.