Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 542
Chiah Soon Avatar asked Dec 26 '19 04:12

Chiah Soon


People also ask

Can two documents have the same ID in firestore?

You cannot have several documents with the same ID in one Collection: a document ID must be unique across a collection.

Is firebase document ID unique?

The names of documents within a collection are unique. You can provide your own keys, such as user IDs, or you can let Cloud Firestore create random IDs for you automatically.

What is firestore document ID?

On this page. Inherited Method Summary. public abstract @interface DocumentId implements Annotation. Annotation used to mark a POJO property to be automatically populated with the document's ID when the POJO is created from a Cloud Firestore document (for example, via toObject(Class) ).

How do I get the document ID added to firestore?

Set the data of a document within a collection, explicitly specifying a document identifier. Add a new document to a collection. In this case, Cloud Firestore automatically generates the document identifier. Create an empty document with an automatically generated identifier, and assign data to it later.

How do I assign data to a document in Cloud Firestore?

Set the data of a document within a collection, explicitly specifying a document identifier. Add a new document to a collection. In this case, Cloud Firestore automatically generates the document identifier. Create an empty document with an automatically generated identifier, and assign data to it later.

How to update a count for a specific page in FireStore?

Another option is to use a Firestore Cloud Function to update a count for each newly-created document. This is especially useful if you want to query a specific page in paginated query. Goal: Maintain a sequential count on each document in the collection. The first document is number: 1, second number: 2, and so on.

What types of data can be stored in Firestore?

Data types. Cloud Firestore lets you write a variety of data types inside a document, including strings, booleans, numbers, dates, null, and nested arrays and objects. Cloud Firestore always stores numbers as doubles, regardless of what type of number you use in your code.

Does Cloud Firestore support auto-generated docids with auto ordering?

For example, you may want a field that increases by 1 after each new document is created. Cloud Firestore does not provide automatic ordering on the auto-generated docIDs, so we need be clever to solve this problem .


Video Answer


3 Answers

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.

like image 163
Doug Stevenson Avatar answered Oct 16 '22 20:10

Doug Stevenson


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.

like image 45
Frank van Puffelen Avatar answered Oct 16 '22 21:10

Frank van Puffelen


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.

like image 2
Waelmas Avatar answered Oct 16 '22 19:10

Waelmas