0

I'm using Flutter and Firestore, and I want to be able to create documents with an assigned ID field inside them, but I don't know how to make that the new document IDs Field is equal to the last document ID field number + 1.

For Example, if I have this field inside a document, I want to make the next document's correlativeNumber equal to this one + 1 = 87

Example field

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
nmshu
  • 11
  • 5
  • Sounds like a fun use-case. Did you try any part of the implementation already? This would probably be a good place to start: https://stackoverflow.com/questions/55081634/firestore-and-auto-increment-ids – Frank van Puffelen Oct 03 '22 at 03:26

1 Answers1

0

The best option that you have is to store the last correlativeNumber into a document. Each time a new document is added, increment that number by 1. In this way, you can always know which number was used previously.

But there is something that should take into consideration. When it comes to document IDs, according to the official documentation:

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.

So it's best to use the Firestore's built-in identifiers, which are by definition completely unique.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193