问题描述
我目前正在将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和自动递增ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!