Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Firestore document's auto-generated ID versus using a custom ID

I'm currently deciding on my Firestore data structure.

I'll need a products collection, and the products items will live inside of it as documents.

Here are my product's fields:

  • uniqueKey: string
  • description: array of strings
  • images: array of objects
  • price: number

QUESTION

Should I use Firestore auto-generated ID's to be the ID of my documents, or is it better to use my uniqueKey (which I'll query for in many occasions) as the document ID? Is there a best option between the 2?

I imagine that if I use my uniqueKey, it will make my life easier when retrieving a single document, but I'll have to query for more than 1 product on many occasions too.

Using my uniqueKey as ID:

db.collection("products").doc("myUniqueKey").get();

Using my Firestore auto-generated ID:

db.collection("products").where("uniqueKey", "==", "myUniqueKey").get();

Is this enough of a reason to go with my uniqueKey instead of the auto-generated one? Is there a rule of thumb here? What's the best practice in this case?

like image 630
cbdeveloper Avatar asked May 27 '19 17:05

cbdeveloper


People also ask

How to create custom document ID when adding data to FireStore?

Run the setDoc () query… And you can see a brand new document has been created inside the countries collection with an auto-generated document ID. With the setDoc () method, we can create our own custom document ID when adding data to Firestore database. Instead of using the collection () method, we need to use the doc () method.

How to add a document to the firebase version 9 Cloud Firestore?

There are two methods that we can use to add document data to the Firebase Version 9 Cloud Firestore. Using the setDoc () method, you can add a document to the Firestore database by creating: In order to use the setDoc () method, we need to import three methods from the Firebase Firestore Import Statement.

How to store a JavaScript object as document data in FireStore?

The second argument of the setDoc () method is the actual data that we want to store as a document inside the countries collection. The good news is that we can simply store a JavaScript object as document data inside the Firestore Database. So let’s create a JavaScript object with a couple of key-value pairs, aka properties.

How to add document data with auto-generated ID in PHP?

// Add document data with auto-generated id. // Add a new document with a generated id. printf ( 'Added document with ID: %s' . PHP_EOL, $addedDocRef -> id ());


1 Answers

In terms of making queries from a client, using only the information you've given in the question, I don't see that there's much practical difference between a document get using its known ID, or a query on a field that is also unique. Either way, an index is used on the server side, and it costs exactly 1 document read. The document get() might be marginally faster, but it's not worthwhile to optimize like this (in my opinion).

When making decision about data modeling like this, it's more important to think about things like system behavior under load and security rules.

If you're reading and writing a lot of documents whose IDs have a sequential property, you could run into hotspotting on those writes. So, if you want to use your own ID, and you expect to be reading and writing them in that sequence under heavy load, you could have a problem. If you don't anticipate this to be the situation, then it likely doesn't matter too much whose ID you use.

If you are going to use security rules to limit access to documents, and you use the contents of other documents to help with that, you'll need to be able to uniquely identify those documents in your rule. You can't perform a query against a collection in rules, so you might need meaningful IDs that will give direct access when used by rules. If your own IDs can be used easily this way in security rules, that might be more convenient overall. If you're force to used Firestore's generated IDs, it might become inconvenient, difficult, or expensive to try to maintain a relationship between your IDs and Firestore's IDs.

In any event, the decision you're making is not just about which ID is "better" in a general sense, but which ID is better for your specific, anticipated situation, under load, with security in mind.

like image 198
Doug Stevenson Avatar answered Sep 30 '22 18:09

Doug Stevenson