28

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

4 Answers 4

80

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.

Sign up to request clarification or add additional context in comments.

2 Comments

At the moment, pushing IDs in the console is not possible. I will either use a set or just do the push from the app. Thanks.
So can you use the Firebase console to populate the array or not!?
14

To add arrays manually using Firebase Realtime DB console:

  • Use double " " instead of single ' ' quotes

enter image description here


Which provides this structure:

enter image description here

1 Comment

I was using single quotes then I changed to double quotes and it worked!!
6

After writing my other answer I realized that you might simply be looking how to add push IDs in the console.

That's not a feature at the moment. Most of is either use different types of keys when entering test data or have a little JavaScript snippet in another tab to generate the keys and copy/paste them over.

Please do request the feature here, since you're definitely not the first one to ask.

Comments

1

I had same problems with that few weeks ago, but I found it in here. Finally I can use it with my ChartJS.

function jamToArray(snapshot) {
    const returnArr = [];
    snapshot.forEach(function(childSnapshot) {
        const item = childSnapshot.val().time;
        returnArr.push(item);
    });
    return returnArr;
};

firebase.database().ref('sensor').limitToLast(10).on('value',(snapshot) => {
    const jam = jamToArray(snapshot);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.