3

What i mean- if we collect all the ids(auto-generated) from the all collections and sub-collections and etc. Will all of them unique?

What for? I'am building a new feature, where users can share their items from sub-collections, so I am not sure - can I use same id between users sub-collections.

Thanks!

Valera Kvip
  • 334
  • 5
  • 14

1 Answers1

13

Firestore document IDs must be unique within the same collection. They can be duplicated in other collections or subcollections. You can test this for yourself by manually creating the documents and see that it works OK.

Practically speaking, all automatically generated document IDs will be unique, because they are random, and there is an astronomically low chance of two of them colliding. The client SDK generates ID with 20 characters, each allowing for all lowercase letters, all uppercase letters, and all digits. So there are a total of (26+26+10)^20 = 7.0e35 possible combinations of possible unique IDs. (That's a very large number - 7 with 35 zeroes after it.)

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I expected this to be so but was surprised to see two documents in one of my collections to have the same id. After some efforts I realized one of the ids was starting with a `space` and the other was not, and hence they were actually different. However, the space didn't show up in the firebase web view which made it difficult to figure out the issue. So I guess its letters + upper case letters + digits + some symbols. – Srujan Barai Oct 05 '20 at 03:25
  • 1
    @SrujanBarai Where did this space come from? :O – mesqueeb Dec 01 '20 at 01:37
  • the fact that it came so close as the be one space char away from being identical is kinda scary for security and other obvious concerns... – Uriel Bitton Feb 22 '23 at 20:52