如何在不获取全部信息的情况下获取 Firebase 中元素/列表的大小? [英] How to get size of an element/list in Firebase without get it all?
问题描述
我需要在 Firebase 中测量元素或列表的大小/长度,但我不需要真正的内容.
I need to measure the size/length of an element or list in Firebase but I don't need the real content.
类似firebase.database().ref("users/").once("length" || "size").then(callback)
ps Javascript SDK
ps Javascript SDK
推荐答案
Firebase 没有计数运算符,因此唯一的方法是下载所有子项或保持一个单独的 <children>_count
属性同步.后者不是一项微不足道的任务(参见我的 在这里回答一种方法 和这个示例云函数),因此大多数情况下,开发人员最终可能会采用下载量过多但数据量不大的方法:
Firebase doesn't have a count operator, so the only way is to download all children or keep a separate <children>_count
property in sync. The latter is not a trivial task (see my answer here for one approach and this example Cloud Function), so most often developers likely end up going with the downloads-too-much-data-but-is-trivial approach:
ref.child("messages").on("value", function(snapshot) {
console.log("There are "+snapshot.numChildren()+" messages");
})
计算子级数的更有效方法是使用 shallow=true
参数触发 REST 调用,它只会为您提供密钥.见 在 Firebase 中,有没有办法在不加载所有节点数据的情况下获取节点的子节点数量?
A more efficient way to count the children would be to fire a REST call with shallow=true
parameter, which will give you just the keys. See In Firebase, is there a way to get the number of children of a node without loading all the node data?
这篇关于如何在不获取全部信息的情况下获取 Firebase 中元素/列表的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!