在 Firebase 中,有没有办法在不加载所有节点数据的情况下获取节点的子节点数? [英] In Firebase, is there a way to get the number of children of a node without loading all the node data?

查看:27
本文介绍了在 Firebase 中,有没有办法在不加载所有节点数据的情况下获取节点的子节点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以通过

firebase_node.once('value', function(snapshot) { alert('Count: ' + snapshot.numChildren()); });

但我相信这会从服务器获取该节点的整个子树.对于庞大的列表,这似乎是 RAM 和延迟密集型的.有没有办法在不获取整个内容的情况下获取计数(和/或子名称列表)?

But I believe this fetches the entire sub-tree of that node from the server. For huge lists, that seems RAM and latency intensive. Is there a way of getting the count (and/or a list of child names) without fetching the whole thing?

推荐答案

您提供的代码片段确实加载了整个数据集,然后在客户端对其进行计数,这对于大量数据可能会非常慢.

The code snippet you gave does indeed load the entire set of data and then counts it client-side, which can be very slow for large amounts of data.

Firebase 目前无法在不加载数据的情况下计算孩子的数量,但我们确实计划添加它.

Firebase doesn't currently have a way to count children without loading data, but we do plan to add it.

目前,一种解决方案是维护一个孩子数量的计数器,并在每次添加新孩子时更新它.您可以使用事务来计算项目,例如跟踪 upvodes 的代码:

For now, one solution would be to maintain a counter of the number of children and update it every time you add a new child. You could use a transaction to count items, like in this code tracking upvodes:

var upvotesRef = new Firebase('https://docs-examples.firebaseio.com/android/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes');
upvotesRef.transaction(function (current_value) {
  return (current_value || 0) + 1;
});

有关详细信息,请参阅 https://www.firebase.com/docs/transactions.html

For more info, see https://www.firebase.com/docs/transactions.html

更新:Firebase 最近发布了 Cloud Functions.使用 Cloud Functions,您无需创建自己的服务器.您可以简单地编写 JavaScript 函数并将其上传到 Firebase.Firebase 将负责在事件发生时触发函数.

UPDATE: Firebase recently released Cloud Functions. With Cloud Functions, you don't need to create your own Server. You can simply write JavaScript functions and upload it to Firebase. Firebase will be responsible for triggering functions whenever an event occurs.

例如,如果您想统计点赞数,您应该创建一个类似于此的结构:

If you want to count upvotes for example, you should create a structure similar to this one:

{
  "posts" : {
    "-JRHTHaIs-jNPLXOQivY" : {
      "upvotes_count":5,
      "upvotes" : {
      "userX" : true,
      "userY" : true,
      "userZ" : true,
      ...
    }
    }
  }
}

然后编写一个javascript函数,当有新的写入upvotes节点时增加upvotes_count.

And then write a javascript function to increase the upvotes_count when there is a new write to the upvotes node.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.countlikes = functions.database.ref('/posts/$postid/upvotes').onWrite(event => {
  return event.data.ref.parent.child('upvotes_count').set(event.data.numChildren());
});

您可以阅读文档以了解如何开始使用云函数.

You can read the Documentation to know how to Get Started with Cloud Functions.

此外,这里还有一个计算帖子的例子:https://github.com/firebase/functions-samples/blob/master/child-count/functions/index.js

Also, another example of counting posts is here: https://github.com/firebase/functions-samples/blob/master/child-count/functions/index.js

firebase 文档 发生了变化,而不是 event 我们现在有 changecontext.

The firebase docs have changed so instead of event we now have change and context.

给定的示例抛出一个错误,抱怨 event.data 未定义.这种模式似乎效果更好:

The given example throws an error complaining that event.data is undefined. This pattern seems to work better:

exports.countPrescriptions = functions.database.ref(`/prescriptions`).onWrite((change, context) => {
    const data = change.after.val();
    const count = Object.keys(data).length;
    return change.after.ref.child('_count').set(count);
});

```

这篇关于在 Firebase 中,有没有办法在不加载所有节点数据的情况下获取节点的子节点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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