Firestore 和自动增量 ID [英] Firestore and Auto Increment Ids

查看:25
本文介绍了Firestore 和自动增量 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将 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:

  • 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆