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

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

问题描述

Firestore 文档,我们得到Firestore 的最大尺寸文档是:

文档的最大大小 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天全站免登陆