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:
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:
Am I doing it right?
Does
0
s and1
s in the image is normal?If there is better way, how to improve my code so that Firebase can construct better data structure for me?
Does below structure possible?
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.
Another structure to consider is
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
Do not use 0's and 1's as node 'names'.