Firestore and Auto Increment Ids

后端 未结 1 500
没有蜡笔的小新
没有蜡笔的小新 2020-12-07 01:59

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 con

相关标签:
1条回答
  • 2020-12-07 02:54

    There is no built-in auto-increment operator in Firestore. If you want something like that, you will have to build it yourself.

    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.

    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)

    And this page in the Firestore documentation in distributed counters.

    0 讨论(0)
提交回复
热议问题