2

Data Structure

Countries (Collection)
|
--- 5nHsOxXPPenciyejILWP (Document)
    |
    --- Country Name (Map)
        |
        --- en (String) => Germany
        --- ar (String) => ألمانيا
        --- es (String) => Alemania
        --- it (String) => Germania
        --- de (String) => Deutschland
        --- ru (String) => Германия
    --- Cities (Map)
        |
        --- Berlin (String) => Berlin
        --- Munich (String) => Munich
        --- Frankfurt (String) => Frankfurt

Posts (Collection)
|
--- RhwFwkvSZ2WCh2K1O7xg (Document)
    |
    --- Title (String) => ...
    --- Description (String) => ...
    --- Country ID (String) => 5nHsOxXPPenciyejILWP
    --- City ID (String) => Berlin

In the Cities field, I want the keys to be unique IDs such as b4ejx8nUIFrXE0EkVrbs instead of using cities names.

I'm currently make it like this Berlin (String) => Berlin but I want it to be like this b4ejx8nUIFrXE0EkVrbs (String) => Berlin and in Posts collection it must be like this City ID (String) => b4ejx8nUIFrXE0EkVrbs instead of City ID (String) => Berlin.

I can solve the problem by creating a subcollection inside the Countries collection and every city in this situation will have a unique ID but this scenario will cost me a lot of reads instead of 1 read-only.

Is there any way to create a unique id that looks like this SKwFaGBYimiFBLUbZ9Oe, aXNTJsW6f6nTOsU7NBNO, EG1lo3Bbcnv9SJwLUGt5, q4QdEz3QPpvWu39gwpuK, etc... instead of cities names?

Taha Sami
  • 1,565
  • 1
  • 16
  • 43

1 Answers1

3

You can always generate your own IDs and use them while adding the data. There are packages like UUID or you can even get those random IDs from Firebase SDK like this:

const randomId = firebase.firestore().collection('users').doc().id
// use this randomId while adding data
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • Did you mean I can get a unique ID from Firebase SDK even without creating a document? Because I know I can get a unique ID after creating the document! – Taha Sami Oct 31 '21 at 06:50
  • @A998817 yes you can. You can generate the random ID in any way and use it while adding the data. Can you share the code that adds cities there? It might be easier to explain that way . – Dharmaraj Oct 31 '21 at 06:55
  • I'm not starting code yet because I want to draw data structure first to make it easy while programming. By the way, during I get unique IDs from Firebase SDK is it possible to get duplicate IDs from the Android app or admin panel because that will cause a big problem if the IDs are duplicated? I appreciate your help, Thank you. – Taha Sami Oct 31 '21 at 07:05
  • @A998817 I have not heard of duplicated IDs yet so you should be good to go. or you can try using UUIDs they are random for sure. Checkout this answer for more info: https://stackoverflow.com/questions/1155008/how-unique-is-uuid – Dharmaraj Oct 31 '21 at 07:08
  • In my opinion that is not possible to get duplicate IDs from Firebase SDK. Thanks for your help ❤️️ – Taha Sami Oct 31 '21 at 07:14