Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting arrays in Firebase using Firebase console

Tags:

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. enter image description here

like image 719
vijayst Avatar asked Oct 15 '16 06:10

vijayst


People also ask

Can you have arrays in Firebase?

React and Firebase with Hooks For Absolute Beginners This is because Firebase does not support Arrays directly, but it creates a list of objects with integers as key names.

Does Firebase scale automatically?

Lastly, it'll scale massively and automatically. Firebase Functions will just do it automatically for you based on the traffic you receive and at an incredibly low cost.

How do I manually add data to Firebase?

If you open your app from Firebase dashboard, you can add data manually by clicking on the + sign. We will create a simple data structure.


1 Answers

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" } 

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"] }); 

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.

Don't use an array, when you actually need a set

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.

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.

like image 102
Frank van Puffelen Avatar answered Oct 02 '22 13:10

Frank van Puffelen