Chiah Soon
Chiah Soon

Reputation: 587

Firestore: using Document ID vs Id as a field

I've been curious for a long time now, but is it advisable to save an id field within a document in firestore? Since we already have a unique documentId store in firestore?

Further, when authenticating users with email, the results return a uid, and in a tutorial online it was saved as a field in the user document. I am confused as to why this was necessary since we already have the documentId of the user.

Upvotes: 13

Views: 5220

Answers (3)

Waelmas
Waelmas

Reputation: 1962

The answers provided by Doug and Frank will probably answer your questions.

I have been working on an app recently and was unsure which approach I should use, making a query based on a "key" field or based on the document ID itself.

And it appears like even if the "key" field approach might be the logical approach, using the document ID directly will be way faster when your database becomes large. This makes sense as you will not have to search through all the docs every time.

So my advice would be, unless absolutely needed for your use-case, try to avoid searching based on a field that has the document ID as it will slow things down when your database grows.

Upvotes: 2

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

I don't know if it's advisable to store the document ID in the document itself too, and I never do it myself. But that said, it is quite common to do so, and mostly harmless. When the ID is duplicated it's often a matter of convenience, since then all information is present in the document's data already. And as said: I don't think there's anything badly wrong with it, but it's just a matter of personal preference.

Upvotes: 3

Doug Stevenson
Doug Stevenson

Reputation: 317392

You are only required to store the document ID in the document if it satisfies some query you would like to make with that ID. A vast majority of the time that I see, storing the ID is simply not required, or at best, just a convenience.

The only time I can think of where the ID is necessary to have in the document is if you're looking for a document of a particular ID during a collection group query. Since these types of queries can only consider normal field values as a filter, you'd need the ID in the document to satisfy that query. Otherwise, it's not possible (you can't use the special FieldPath.documentId() token in a collection group query).

Putting the document ID in the document can be a convenience if you're using some form of serialization technique to automatically map the document fields into some object properties based on their name. Most object mappers wouldn't know how to find the document ID, since it won't normally end up in the map of fields in the raw document data. But on Android, this is actually now possible using the relatively new @DocumentId annotation along with Firestore's built-in POJO object mapper.

I don't know what this tutorial is trying to actually achieve by putting the document ID in a field, but it's really just not necessary. You should ask the author why.

Upvotes: 8

Related Questions