1

Is there a way to do this ? Or with oderByValue ? Any other mean ?

fb.orderByChild('upvotes - downvotes').startAt(_start).endAt(_end).limitToLast(_n).on("child_added", function(dataSnapshot) {
          data.push(dataSnapshot.val());
       });

P.S.: Yes, I already though about creating a third entry in the database that keeps track of (upvotes-downvotes), but that is what I would like to avoid here.

Coder1000
  • 4,071
  • 9
  • 35
  • 84

1 Answers1

3

No, orderByChild does exactly what its name says. Currently, there is no way to define such "computed" nodes neither in the data model, nor in query criteria.

Therefore, you should store the difference directly in the database, set an index on it using security rules, and use that for your query. Updating the vote counts with a transaction should help a lot to implement it in a clean way.

vzsg
  • 2,831
  • 17
  • 20
  • I am aware of what orderByChild does, but I needed some way to express my question. I am already updating the vote count with a transaction. But I have 2 different variables: upvote and downvote. Would I really need to create a third variable and update it each time upvote and downvote are updated ? There must be a more elegant solution. – Coder1000 Nov 07 '16 at 08:46
  • Unfortunately yes, you need the third variable. Such a limitation is not uncommon with non-SQL databases. Personally I think that if you use a transaction on the post node to manage the counters, it's not inelegant. – vzsg Nov 07 '16 at 08:48
  • I thank you for your answer, but I would like to wait and see if there is not a better option since I already thought of this one and precisely didn't want to use it because of bandwith waste :/ – Coder1000 Nov 07 '16 at 08:49
  • I don't see what bandwidth is wasted here, but it's your call. – vzsg Nov 07 '16 at 08:50
  • Well, maybe my understanding of bandwidth usage is wrong, but wouldn't a new third transaction that modifies the total score vote each time downvote and upvote are modified use more bandwidth ? – Coder1000 Nov 07 '16 at 08:52
  • No, if you consider transactions as what they are in the RTDB. See: https://firebase.google.com/docs/database/web/read-and-write#save_data_as_transactions. One transaction is enough to update both counters (upvotes+score or downvotes+score, depending on the action). – vzsg Nov 07 '16 at 08:54
  • I know, but doesn't a query that needs to update more data use more bandwidth as a result ? It seems counterintuitive :/ You are stills sending 2 actions to be performed by the server instead of 1. That's a 100% increase in use for transactions, no ? I would like my code to be as optimised as possible to reduce costs, that is precisely why I asked my question. – Coder1000 Nov 07 '16 at 08:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127521/discussion-between-vzsg-and-coder1000). – vzsg Nov 07 '16 at 08:59
  • Well, looks like you are (sadly) right :/ I accept your answer :) – Coder1000 Nov 10 '16 at 20:37