使用 Firebase 控制台在 Firebase 中设置数组 [英] Setting arrays in Firebase using Firebase console

查看:27
本文介绍了使用 Firebase 控制台在 Firebase 中设置数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Firebase 控制台为演示应用准备数据.数据项之一是参加者.与会者是一个数组.我想在 Firebase 中添加一些与会者作为数组.我知道 Firebase 没有数组,而是带有键的对象(按时间顺序).我该如何准备样本数据?我当前的 Firebase 数据如下所示.

I am using Firebase console for preparing data for a demo app. One of the data item is attendees. Attendees is an array. I want to add a few attendees as an array in Firebase. I understand Firebase does not have arrays, but object with keys (in chronological order). How do I do that for preparing sample data? My current Firebase data looks like the below.

推荐答案

Firebase 数据库不存储数组.它存储字典/关联数组.所以你能得到的最接近的是:

The Firebase Database doesn't store arrays. It stores dictionaries/associate arrays. So the closest you can get is:

attendees: {
  0: "Bill Gates",
  1: "Larry Page",
  2: "James Tamplin"
}

您可以在 Firebase 控制台中构建此结构.然后,当您使用其中一个 Firebase SDK 读取它时,它将被转换为一个数组.

You can build this structure in the Firebase Console. And then when you read it with one of the Firebase SDKs, it will be translated into an array.

firebase.database().ref('attendees').once('value', function(snapshot) {
  console.log(snapshot.val());
  // ["Bill Gates", "Larry Page", "James Tamplin"]
});

所以这可能就是您要寻找的结果.但我建议您阅读这篇博客文章,了解如果您不存储数组,为什么 Firebase 更喜欢它:https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html.

So this may be the result that you're look for. But I recommend reading this blog post on why Firebase prefers it if you don't store arrays: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html.

大多数开发人员实际上并没有尝试存储数组,我认为您的情况可能就是其中之一.例如:比尔盖茨"可以两次成为与会者吗?

Most developers are not actually trying to store an array and I think your case might be one of those. For example: can "Bill Gates" be an attendee twice?

attendees: {
  0: "Bill Gates",
  1: "Larry Page",
  2: "James Tamplin",
  3: "Bill Gates"
}

如果没有,您将不得不在添加他之前检查他是否已经在数组中.

If not, you're going to have to check whether he's already in the array before you add him.

if (!attendees.contains("Bill Gates")) {
  attendees.push("Bill Gates");
}

这清楚地表明您的数据结构对于用例来说是次优的.在添加新子项之前必须检查所有现有子项会限制可扩展性.

This is a clear sign that your data structure is sub-optimal for the use-case. Having to check all existing children before adding a new one is going to limit scalability.

在这种情况下,您真正​​想要的是一个集合:一种数据结构,其中每个子项只能出现一次.在 Firebase 中,您可以像这样对集合进行建模:

In this case, what you really want is a set: a data structure where each child can be present only once. In Firebase you model sets like this:

attendees: {
  "Bill Gates": true,
  "Larry Page": true,
  "James Tamplin": true
}

现在,无论您何时尝试第二次添加比尔盖茨,都无济于事:

And now whenever you try to add Bill Gates a second time, it's a no-op:

attendees["Bill Gates"] = true;

因此不必为唯一性要求编码,数据结构隐式地解决了它.

So instead of having to code for the uniqueness requirement, the data structure implicitly solves it.

这篇关于使用 Firebase 控制台在 Firebase 中设置数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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