11

SITUATION:

I currently load the latest posts chronologically with this code:


CODE:

        $scope.data = [];
        var _n = Math.ceil(($(window).height() - 50) / (350)) + 1;
        var _start = <%= lastID %>;
        var firstElementsLoaded = false;

        $scope.getDataset = function() {

            fb.orderByChild('id').startAt(_start).limitToFirst(_n).on("child_added", function(dataSnapshot) {

                _start = dataSnapshot.child("id").val() + 1;
                console.log("LAST TRUE ID:"+ _start);

                $scope.data.push(dataSnapshot.val());
                $scope.$apply()
                console.log("THE VALUE:"+$scope.data);

                firstElementsLoaded = true;
            });
        };


        $scope.getDataset();

QUESTION:

How should I modify it to essentially have the exact same behaviour but to only query posts with scores above 100 according to their chronological order ?


UPDATE:

I have finally found a way to use the "create another index" method with my architecture. But one hurdle remains:

How to copy a childNode from one node to another?

Community
  • 1
  • 1
Coder1000
  • 4,071
  • 9
  • 35
  • 84
  • That would require ordering/filtering by two properties, which is not possible. You may be able to combine the two values into a single property, as mentioned here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Dec 29 '16 at 20:00
  • @FrankvanPuffelen Thank you for the quick response time. I looked at the answer you linked but I am still confused as to how I would merge my IDs and scores into one property in my particular case. Before I investigate further, does it seem doable to you when you look at my code in my question ? – Coder1000 Dec 29 '16 at 20:06
  • 1
    It sounds tricky, since you essentially need two relatively operations (>100 and orderByKey). If you could bucket the >100 items into a separate top-level node, it would be trivial of course. – Frank van Puffelen Dec 29 '16 at 20:11
  • @FrankvanPuffelen Indeed, but I am trying to find a more elegant solution than having to put posts into a different node once they reach 100 and then having to put them back in the normal node in case their score goes under 100 at some point. It seems very inefficient :/ and can lead to many many hurdles due to my current architecture. This is sadly not possible in this case :( – Coder1000 Dec 29 '16 at 20:13
  • There are ways to combine two values in a way to allows filtering ordering on them both in a relative fashion, but they're quite involved. The most common one is in [Geohash values](https://en.wikipedia.org/wiki/Geohash) (as uses in GeoFire to query a lon/lat range). If your app requires this functionality and other solutions are not acceptable, then I'd look into combining your values in a similar way. – Frank van Puffelen Dec 29 '16 at 20:26
  • @FrankvanPuffelen Could you please develop your comment into an answer that is my code transformed to use a working solution with an explanation so I can accept your answer and future Firebase users will be able to refer to this ? – Coder1000 Dec 29 '16 at 20:31
  • My comment is not an answer and definitely not working code. To be honest, I doubt anyone will answer with working code since the topic of combining multiple values into a single value is broad and complex. But if somebody is (or you are) up for it, I agree it would be a great answer for others too. – Frank van Puffelen Dec 29 '16 at 21:07
  • @FrankvanPuffelen You are one of the most qualified people on SO to answer this question. If you are unable to answer, the chances that someone else will are close to 0, no ? :( Do you think Kato or another Firebase engineer would be able to answer ? – Coder1000 Dec 29 '16 at 21:25
  • A hint I would give is to keep the way you load it, but when you're about to display, check if the score is above 100 or not. And only display the ones above 100. Keep in mind that this is not recommended because you're downloading lots of unecessary data. – Rosário Pereira Fernandes Dec 29 '16 at 23:12
  • @RosárioPereiraFernandes Which is precisely what I don't want :/ I thought about it, but it makes no sense. – Coder1000 Dec 30 '16 at 10:37

1 Answers1

3

I rethought my architecture and created a specific index for top posts:

if (funPost.score >= global.hotNumber && funPost.hot == false) {
            var hotPostRef = firebase.database().ref("hot/section/"+key);
            var hotPost = {
                title: funPost.title,
                image: funPost.image,
                id: funPost.id,
                key: funPost.key
            }
            hotPostRef.set(hotPost);
            funPostRef.update({"hot": true});
        }
   else if (funPost.score <= (global.hotNumber - 25) && funPost.hot == true) {
        var hotPostRef = firebase.database().ref("hot/section/"+key);
        hotPostRef.remove();
        funPostRef.update({"hot": false});
   }
Coder1000
  • 4,071
  • 9
  • 35
  • 84