0

Firestore constrains Documents to 1MB, including field names and document IDs.

Well, as far as I've tested, the generated IDs for created users and documents were about 50 Bytes long(I'm modeling the data in top level collections), but in the documentation apparently it could be as high as 1.5KB, which is a huge difference.

This directly impacts how I model the database, since I have an array of IDs inside a document and a counter. I have to know how many IDs to store.

Also, replacing the array by a collection with each ID as a single document inside is not a reasonable solution, it'd cost too much.

Can I assume that for a given collection, since my tests are all giving the same range of less than 50B for IDs,it won't suddenly create many IDs with 1.5KB ?

Without assuming that, the amount of IDs I can store in the array inside a document goes from around 20 thousand (if each ID has around 50B) to as little as 660 (if each ID has around 1.5KB).

Firebase does not offer a way to analyse how big a document is nor how many bytes an autogenerated document ID will have for a given collection.

I tested many different collections and have many users already. All of those autogenerated IDs where less than 50 Bytes long, so I'm assuming this is a pattern, but not convinced about it since there is no guarantee from the documentation.

  • "*Also, replacing the array by a collection with each ID as a single document inside is not a reasonable solution, it'd cost too much.*" Actually this is the data modeling solution for Firestore in order to avoid exceeding the max size of a document. Array fields should not be able to grow unbounded in Firestore - there needs to be a reasonable cap, and if you can't cap it, you should use a collection of documents instead. If this solution doesn't work for you, then perhaps Firestore isn't the right database for your application. – Doug Stevenson May 11 '23 at 00:08
  • You should also know that you can give documents any ID you want that's unique in the collection. You don't need to accept the random IDs generated by the SDK. Even if you do accept them, you should know that those random IDs never change size. There is a *limit* to the size of an ID, but the SDK will never generate one bigger than the ones it already generates. The length they have now is virtually guaranteed to be unique due to their size. – Doug Stevenson May 11 '23 at 00:10

1 Answers1

0

Can I assume that for a given collection, since my tests are all giving the same range of less than 50B for IDs,it won't suddenly create many IDs with 1.5KB ?

Firebase does not offer a way to analyse how big a document is nor how many bytes an autogenerated document ID will have for a given collection.

The Firestore SDKs always generate random document IDs of the same length. They are not generated by the cloud service. If you don't trust this, you can always generate your own IDs and add the document using set rather than add.

See:

You can even look at the source code to verify this yourself. Here's a link to JavaScript source that caps the length at 20 characters:

See:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441