如何获得Firestore文件大小? [英] How to get a Firestore document size?

查看:62
本文介绍了如何获得Firestore文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firestore文档中,我们可以得出 Firestore的最大大小文档是:

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

文档 1 MiB 的最大大小(1,048,576字节)

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

问题

我如何知道单个文档的当前大小,以检查是否接近 那个1mb的限制?

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

示例:

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.

这篇关于如何获得Firestore文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆