1

Should I create a different unique identifier to use in the frontend or can I use the auto generated Firestore ID? For example, I have a page that reads the ID in the URL and fetches the correspondent document. Should I use something else or is the id fine?

Jordi Enric
  • 116
  • 4
  • 2
    Using the ID is absolutely fine. Firebase IDs are unique and you should never have a problem when fetching a document – Thales Kenne Feb 16 '21 at 12:41

2 Answers2

1

In my experience using Firestore it is fine to use the auto-generated id.

Something to keep in mind is that you can't add it to a query (e.g. in a where()), you can only retrieve by pointing to the document (db.doc(collection-name/doc-id).get()) in the collection and getting it. This also means that you can't retrieve a subset of fields when retrieving a document using an id. I guess these points go with any id you use, auto-generated or not.

Brettski
  • 19,351
  • 15
  • 74
  • 97
  • 3
    You _can_ use it in a query ```db.collection('books').where('__name__', '==' ,'fK3ddutEpD2qQqRMXNW5').get()``` – SRR Feb 16 '21 at 14:47
  • `__name__` does not just return/match the documentID. It is essentially an alias for .documentID() fieldPath, which represents the FULL PATH to the document;the actual document path is just the last part: https://stackoverflow.com/questions/56149601/firestore-collection-group-query-on-documentid/58104104#58104104 – LeadDreamer Feb 16 '21 at 20:59
  • I didn't realize that existed, thanks folks, much appreciated! – Brettski Feb 17 '21 at 16:10
0

I've been reading about it and as long as the security rules are set it's fine to use the Ids in the frontend.

Jordi Enric
  • 116
  • 4