Firebase: How to improve this data structure?

659 views Asked by At

I'm building a simple AngularJS app to store 2 different things under 1 thing using Firebase. Say, male and female student groups under students.

This is my current Firebase data structure:

enter image description here

And this is my code for Add function:

$scope.add = function() {
            console.log('add button clicked!');

            var ref = new Firebase('https://XXX.firebaseio.com/');

            var onComplete = function(error) {
                if (error) {
                    console.log('Failed to add a new record');
                } else {
                    console.log('Successfully add a new record');
                }
            };

            var order = ref.child("students");
            var newPostRef = order.push([{
                "male": [{
                    "name": "Jose Fowler",
                    "age": 20
                }, {
                    "name": "Earl Murray",
                    "age": 21
                }]
            }, {
                "female": [{
                    "name": "Jessica Gomez",
                    "age": 22
                }, {
                    "name": "Leona Franklin",
                    "age": 23
                }]
            }], onComplete);

            var postID = newPostRef.key();
            console.log("Unique ID: " + postID);
        }

Since I'm new with NoSQL, I found that this data structure looks weird.

My questions:

  1. Am I doing it right?

  2. Does 0s and 1s in the image is normal?

  3. If there is better way, how to improve my code so that Firebase can construct better data structure for me?

  4. Does below structure possible?

enter image description here

Note: I want to have each data set has unique ID. In other words, each student in students has unique ID. I know it doesn't make sense by looking at this students example. Sorry.

2

There are 2 answers

4
Jay On

Another structure to consider is

students
  student_0_id:
     name:
     age:
     gender
  student_1_id:
     name:
     age:
     gender:

the student_x_id is a firebase generated a auto-generated node id (childByAutoId/push)

This structure offers a number of advantages; one of the biggest is that it's 'flat' so queries are easy. So you could say, query for all of the female students between 18 and 20 years old.

Also, since each student now has a uniquely identifying reference, you can reference them from another node. For example, so you wanted to keep track of which students were in the offered classes

Classes
  Algebra
    student_1_id
    student_2_id
  Chemistry
    student_3_id
    student_4_id

Do not use 0's and 1's as node 'names'.

2
Frank van Puffelen On

Firebase stores JSON objects. While you can push in a JavaScript array, it will be stored in the format you see. Note that if you read the data back into your program, it will be converted back into an array.

As Jay pointed out in his (great) answer, arrays are not a recommended data structure for storing data in a distributed environment. As the number of concurrent clients increases, you'll find that having to maintain a so-called monotonously increasing counter becomes a bottleneck. That's why Firebase has a push operation. It has many of the same qualities of array indices (it always increases, so things are automatically ordered), but is guaranteed to be unique across clients. The downside of these push ids is that they're not as easy to understand as regular array indices.

I would change your code to:

var order = ref.child("students");
var men = order.child("male");
var women = order.child("female");
men.push({
    "name": "Jose Fowler",
    "age": 20
});
men.push({
    "name": "Earl Murray",
    "age": 21
});
women.push({
    "name": "Jessica Gomez",
    "age": 22
});
women.push({
    "name": "Leona Franklin",
    "age": 23
});