How to get a Firestore document size?

firestore document size calculation
firestore document size limit
firestore collection size
firestore pricing
firebase
firestore-size npm
firestore limit query
firebase pricing

From Firestore docs, we get that the maximum size for a Firestore document is:

Maximum size for a document 1 MiB (1,048,576 bytes)

QUESTION

How can I know the current size of a single doc, to check if I'm approaching that 1mb limit?

Example:

var docRef = db.collection("cities").doc("SF");

docRef.get().then(function(doc) {
    if (doc.exists) {
        console.log("Document data:", doc.data());
        // IS THERE A PROPERTY THAT CAN DISPLAY THE DOCUMENT FILE SIZE?
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});

The calculations used to compute the size of a document is fully documented here. There is a lot of text there, so please navigate there to read it. It's not worthwhile to copy all that text here.

If you're having to manually compute the size of a document as it grows, my opinion is that you're probably not modeling your data scalably. If you have lists of data that can grow unbounded, you should probably shouldn't be using an list field, and instead put that data in documents in a new collection or subcollection. There are some exceptions to this rule, but generally speaking, you should not have to worry about computing the size of a document in your client code.

Storage size calculations, Firestore offers free quota that allows you to get started at no cost. The free quota Maximum size for a document, 1 MiB (1,048,576 bytes). The size of a JS object in memory is going to be different than the size of a document persisted in Firestore. The implementation of a JS object could vary drastically between various runtimes. I would only use that library as a very rough estimate.

I was looking in the Firebase reference expecting the metadata would have an attribute, but it doesn't. You can check it here.

So my next approach would be to figure the weight of the object as an approximation. The sizeOf library seems to have a reasonable API for it.

So it would be something like:

sizeof.sizeof(doc.data());

I wouldn't use the document snapshot, because it contains metadata, like if there are pending saves. On another hand overestimating could be better in some cases.

[UPDATE] Thanks to Doug Stevenson for the wonderful insight

So I was curious how much the difference would actually be, so with my clunky js I made a dirty comparison, you can see the demo here

Considering this object:

 {
  "boolean": true,
  "number": 1,
  "text": "example"
  }

And discounting the id this is the result:

| Method  | Bytes |
|---------|-------|
| FireDoc | 37    |
| sizeOf  | 64    |

So sizeOf library could be a good predictor if we want to overestimate (assuming calculations are fine and will behave more or less equal for more complex entities). But as explained in the comment, it is a rough estimation.

Usage and limits, Keep the maximum document size downloaded by an individual client under 10 KiB/second. Limit the global document payload. 1 GiB/second. Document name size. The size of a document name is the sum of: The size of each collection ID and document ID in the path to the document; 16 additional bytes; For a document in the subcollection

I've published a npm package that calculates the size of a Firestore document.

Other packages like sizeof or object-sizeof that calculate the size of JS object will not give you a precise result because some primitives in Firestore have different byte value. For example boolean in Js is stored in 4 bytes, in a Firestore document it's 1 byte. Null is 0 bytes, in Firestore it's 1 byte.

Additionally to that Firestore has own unique types with fixed byte size: Geo point, Date, Reference.

Reference is a large object. Packages like sizeof will traverse through all the methods/properties of Reference, instead just doing the right thing here. Which is to sum String value of a document name + path to it + 16 bytes. Also, if Reference points to a parent doc , sizeof or object-sizeof will not detect circular reference here which might spell even bigger trouble than incorrect size.

Quotas and limits | Firestore, Calculates the approximate size of a Firestore document. from 'firestore-size' const snapshot = db .collection("data") .doc(id) .get(); const data� By default, Cloud Firestore retrieves all documents that satisfy the query in ascending order by document ID, but you can order and limit the data returned. Get all documents in a collection. In addition, you can retrieve all documents in a collection by omitting the where() filter entirely:

Best practices | Firestore, The performance of queries scale with the size of the query result set, not After you make a query that gets documents, the contents of those� To see how Cloud Firestore turns a document and a set of indexes into index entries, see this index entry count example. Maximum size of an index entry: 7.5 KiB. To see how Cloud Firestore calculates index entry size, see index entry size. Maximum sum of the sizes of a document's index entries: 8 MiB

alekslario/firestore-size: Calculates the approximate size of , I need to know the size of a collection to make pagination showing page x of x. I wish to avoid using unnecessary read quotas just to get the size of a collection. By default, Firestore retrieves all documents that satisfy the query in ascending order by document ID, but you can order and limit the data returned. Get all documents in a collection. In addition, you can retrieve all documents in a collection by omitting the where() filter entirely:

The top 10 things to know about Firestore when choosing a , Hi guys, simple question: I want to know the size of a Firestore document without JWT tokens + cookies and getting out of sync with the main browser login? Overview; auth:import and auth:export; Firebase Realtime Database Operation Types; Deploy Targets; Cloud Firestore Index Definition Format

Work around firestore document size limit? - firebase - html, Example. let collectionRef = firestore.collection('col/doc/subcollection'); console. log(`ID of the 42); query.limit(1).get().then(querySnapshot => { querySnapshot. Cloud Firestore provides powerful query functionality for specifying which documents you want to retrieve from a collection or collection group. These queries can also be used with either get() or addSnapshotListener(), as described in Get Data and Get Realtime Updates.

Comments
  • Any plans on releasing the code for these estimations? It would be a good thing for the community to get hold of.
  • There are cases where having such data would be extremely useful. For example in the case of a chat application with a lot of exchanges the messages could be batched into fewer documents to improve read performance, up to a safe value. Current load times of a chat history in Angular for a chat app loading individual responses from Firestore leave much to be desired.
  • Yeah. I was expecting that too. But didn't find anything.
  • I would not trust that sizeof library to be an accurate representation of the size of a Firestore document. The size of a JS object in memory is going to be different than the size of a document persisted in Firestore. The implementation of a JS object could vary drastically between various runtimes. I would only use that library as a very rough estimate. I would expect the estimate to be more accurate in the case of large string values, and less accurate when there are lots of fields of various non-basic types.
  • @DougStevenson updated answer, it was fun, hope to help