Reputation:
I have a form which allows users to submit data to google fireBase DB, I have a separate page which shows a list of who filled in information in the form and what figures they came up with.
I am trying to limit return results to only 1 object with a specific key value, the fireBase documentation says you can use "equalTo" to limit the results.
My issue is users will submit multiple submission over time and object key listed below will have reoccurring values aka "user" how do I go about only showing one of those?
Below is a sample object fireBase return to me.
"abc1234567891012346" : {
"user" : "Frank Albert",
"email" : "[email protected]",
"address" : "123 Some where nice",
"socialspend" : 10,000,
"printimpact" : 180,000
},
"abc1238567891012346" : {
"user" : "James Miller",
"email" : "[email protected]",
"address" : "123 Some where nice",
"socialspend" : 500,
"printimpact" : 24,500
},
"abc1234567891014348" : {
"user" : "Frank Albert",
"email" : "[email protected]",
"address" : "123 Some where nice",
"socialspend" : 10,800,
"printimpact" : 80,000
},
"abc12341267891012346" : {
"user" : "Jessica Smith",
"email" : "[email protected]",
"address" : "123 Some where nicer",
"socialspend" : 2,560,
"printimpact" : 70,800
}
The code which generates the above is the following snippet my codebase
var usersList = firebase.ref('campaignSubmission');
usersList.orderByChild('user').limitToLast(5).once('value')
.then(function(snapshot){
var cpSumission = snapshot.val();
keys = Object.keys(cpSumission);
console.log(cpSumission);
for(var i = 0; i < keys.length; i++) {
var k = keys[i];
// Store whats needed for displaying later
var listEmployeeName = cpSumission[k].user;
var listSocialSpend = cpSumission[k].printimpact;
...
}
}
Upvotes: 0
Views: 497
Reputation: 599686
There is way to do that without custom code (either client-side or in Cloud Functions). As usual with NoSQL databases, the solution is to augment your data model for the use-case you have. In this case it sounds like you want to read the latest submission for each user, so that's what I'd model in the database too:
"latestSubmissionByUserName": {
"Frank Albert": {
"uid": "abc1234567891012346",
"email" : "[email protected]",
"address" : "123 Some where nice",
"socialspend" : 10,000,
"printimpact" : 180,000
}
"James Miller": {
"uid": "abc1238567891012346",
"email" : "[email protected]",
"address" : "123 Some where nice",
"socialspend" : 500,
"printimpact" : 24,500
},
"Jessica Smith": {
"uid": "abc12341267891012346",
"email" : "[email protected]",
"address" : "123 Some where nicer",
"socialspend" : 2,560,
"printimpact" : 70,800
}
}
Since we're using the user
property as the key here, there can by definition be no duplicates. So reading the latest submission for each user becomes as easy as:
var usersList = firebase.ref('latestSubmissionByUserName');
usersList.orderByKey().once('value').then(function(snapshot){
snapshot.forEach(function(userSnapshot) {
console.log(userSnapshot.key);
});
});
Another use-case might be that you want to read the list of users, in which case I'd model precisely that:
"userNames": {
"Frank Albert": true,
"James Miller": true,
"Jessica Smith": true
}
The same code above would read/log the user names from this too.
Both approaches highlight a common theme in NoSQL database in general and Firebase in particular: you'll often end up modeling and duplicating data to allow the use-cases of your app. In addition you sacrifice write performance (since you need to update the duplicated data) for read performance.
To learn more about this I recommend reading NoSQL data modeling and viewing Firebase for SQL developers.
Upvotes: 1