我目前正在将 firestore 用于一个小型应用程序.我想看看我可以用什么来自动增加 id.一个常见的简单用例是订单号.我不想使用文档 ID 的人类消耗品标识符.这对人类来说是不可消耗的.
I am currently using firestore for a small application. I am trying to see what I can use for auto increment ids. A simple use case which is common is order nos. A human consumable identifier I dont want to use the document id. This is not consumable for humans.
推荐答案Firestore 中没有内置的自增运算符.如果你想要这样的东西,你将不得不自己构建它.
There is no built-in auto-increment operator in Firestore. If you want something like that, you will have to build it yourself.
这通常涉及跟踪您在已知位置的文档中使用的最新 ID,然后在事务中从客户端读取和更新该文档.
This typically involves keeping track of the latest ID you've used in a document in a well-known location, and then reading-and-updating that document from the client in a transaction.
但是请认真考虑是否不能使用 Firestore 的内置标识符,因为 Firestore 带有内置标识符而不是自动递增运算符的原因有很多.
But do seriously consider whether you can't use Firestore's built-in identifiers, as there are many reasons why Firestore comes with those built-in instead of having an auto-increment operator.
我强烈建议您查看其中一些相关问题:
I highly recommend checking out some of these related questions:
- 将数字递增为 Firestore 文档名称?
- Firestore 生成的键与集合中的自定义键?
- 如何在 Firebase 中创建自增键?(关于 Firebase 的其他数据库,但原因大致相同)
- Incrementing Number as Firestore Document Name?
- Firestore generated key versus custom key in a collection?
- How to create auto incremented key in Firebase? (about Firebase's other database, but the reasons are largely the same)
以及分布式计数器中的 Firestore 文档中的此页面.
更多推荐
Firestore 和自动增量 ID
发布评论