Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a Firestore document size?

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);
});
like image 881
cbdeveloper Avatar asked May 27 '19 17:05

cbdeveloper


3 Answers

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 probably shouldn't be using a 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.

like image 184
Doug Stevenson Avatar answered Oct 16 '22 23:10

Doug Stevenson


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.

like image 11
Aleks Avatar answered Oct 17 '22 01:10

Aleks


For Android users who want to check the size of a document against the maximum of 1 MiB (1,048,576 bytes) quota, there is a library I have made and that can help you calculate that:

  • https://github.com/alexmamo/FirestoreDocument-Android/tree/master/firestore-document

In this way, you'll be able to always stay below the limit. The algorithm behind this library is the one that is explained in the official documentation regarding the Storage Size.

like image 8
Alex Mamo Avatar answered Oct 17 '22 00:10

Alex Mamo