I have a hypothetical database as bellow
'trees':
'group1': {
'tree1' : {
name: 'A first tree',
startDate: '10/20/2016'
},
'tree2' : {
name: 'A second tree',
startDate: '12/20/2016'
}
},
'group2': {
'tree3' : {
name: 'A third tree',
startDate: '12/20/2016'
},
'tree4' : {
name: 'A fourth tree',
startDate: '5/20/2016'
},
'tree5' : {
name: 'A fifth tree',
startDate: '10/20/2016'
}
}
}
I want to get trees that have startDate equal '5/20/2016' and '12/10/2016' in 'group1' and 'group2'. Ended up with 2 option
For each group, I will go through each child key 'startDate', first query
orderByChild('startDate')
then useequalTo('5/20/2016')
then again with '12/10/2016'var startDates = ['5/10/2016','12/10/2016'] var groups = ['group1','group2'] groups.forEach(function(value, index) function() { firebase.database().ref('trees'+val); startDates.forEach(function(v,i) { userTaskRef.orderByChild('startDate').equalTo(l).once('child_added', function(result) { //some callback code }) })
})
Retrieve all trees in 'group1' and 'group2' to sort out in local
var startDates = ['5/10/2016','12/10/2016'] var groups = ['group1','group2'] groups.forEach(function(value, index) function() { firebase.database().ref('trees'+val); userTaskRef.once('value', function(rt) { // for-each for objects with jQuery $.map(rt.val(), function(value, index){ if (dates.indexOf(value.date) >= 0) { // select this tree } }) }) })
Both working well, I realized that the first method would execute more trips from local to server (number of group * number of startDates). While the second method execute same as number of group but will get all data
So I'd appreciate for any advise for this particular case about performance and security (assume that other tree should not be read when not needed)
Cheers