0

[EDIT: Thanks to @JRLtechwriting, the Storage Size Calculations doc mentioned below has been updated so it no longer mentions namespaces-- Firestore does not (yet?) support them-- and includes more complete examples. After these improvements, my question may not come up again!]

I'm trying to write a general function to calculate the storage size of a Cloud Firestore document, but I'm already stuck on calculating the size of the document's name because I don't know exactly what they mean by a document's "namespace" in the Storage Size Calculations guide:

The size of a document name is the sum of:

  • The namespace string size (if not in the default namespace)
  • The full string size of the document name (integer IDs are 8 bytes each)
  • 16 additional bytes

It also says that the namespace is stored as a string. So, for this hypothetical CFS doc...

var alovelaceDocumentRef = db.collection('users').doc('alovelace');

...which, per the Cloud Firestore Data Model docs, can also be referenced like this...

var alovelaceDocumentRef = db.doc('users/alovelace');

...would the namespace string be 'users'? Or maybe 'users/'? Unfortunately, all of the examples in the Storage Size Calculations guide assume the default namespace (for which the size is 0).

I feel like I should be able to experimentally find the answer to my question, but the only way I can think of to do so is to:

  1. Create a document in a non-default namespace
  2. Track its size in a variable "docSize" using the information in the Storage Size Calculations guide) as I incrementally add data to it
  3. When I get an error message that I have exceeded the maximum document size (1,048,576 bytes, according to the Quotas and Limits guide), subtract docSize from 1,048,576 to get the size of the namespace string

But this approach seems labor-intensive, and probably prone to inaccuracies arising from other limitations of my understanding/knowledge, so I'm hoping one of you more-knowledgeable folks can help. Thanks!

carbonturtle
  • 328
  • 1
  • 9

1 Answers1

2

Firestore does not support different namespaces (see this SO answer) so all documents will be in the default namespace. The namespace string size will always be 0.

I help maintain the Firestore docs so I updated the page.

Juan Lara
  • 6,454
  • 1
  • 22
  • 31
  • Thank you for taking the time to update the [Storage Size Calculations](https://firebase.google.com/docs/firestore/storage-size) doc. Definitely clearer than the old version. I will edit the question to mention this, since my quote from it is now outdated. – carbonturtle Feb 04 '18 at 02:34
  • There's still an apparent inconsistency in the [revised] Storage Size Calculations doc: It says that document names and collection IDs are stored as strings, but later says that numeric IDs take 8 bytes of storage. I've noticed that the JS Firestore client library (as of version 4.9.1) does not allow numeric IDs for collections or documents. So, are numeric IDs possible, or not? If I make a collection or doc whose ID is "3" (a string, at least on the client end), can I count on that ID requiring only 2 bytes of storage? – carbonturtle Feb 06 '18 at 17:59
  • You should be right about document IDs always being strings. Let me double check if there are any cases where you can end up with integer document IDs. – Juan Lara Feb 07 '18 at 00:36