0

I want to get the total number count until specified.

this is my database: database picture

I want to count where ticket_status is not served

ex: I want to count until A6, I will count A1 until A5 where the value of ticket_status" is not served. so the final count will be 3.

is there any filtering kind?

I have this code but it just counts all nodes with the value of "not served"

dbref.child("Queue").child(a.value.toString()).orderByChild("ticket_status").equalTo("not served").addValueEventListener(object : ValueEventListener{
                                override fun onDataChange(snap: DataSnapshot) {
                                        val count = snap.childrenCount
                              dbref.child("Student_Queue_Reserve").child(idNumber.toString()).child("people_ahead").setValue((count-1).toString())
                                }
                                override fun onCancelled(error: DatabaseError) {
                                }
                            })
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
sean
  • 5
  • 2
  • I'm not sure I understand. What exactly in this code doesn't work the way you expect? – Alex Mamo Feb 13 '23 at 15:36
  • the code works fine, it reads and count the child with the "ticket_status" value of "not served". in the screenshot i provided there are 8 childs. 2 of this child have a value of "served" in thier "ticket_status".the code will display as 6 becuase it counts all child with "not served" as value. what i want is i want to read only up until a specified child key. for example i want to read and count where value of "ticket_status" is "not served" up until "A5".(refer to screenshot). A1 and A2 is "served" but "A3","A4","A5" is "not served". i expect an output of 2.2 becuase "A5" is the last node – sean Feb 13 '23 at 15:49

2 Answers2

0

I want to count until A6, I will count A1 until A5 where the value of ticket_status" is not served. So the final count will be 3.

There is no way you can do that in the Realtime Database because queries can only order/filter on a single property. There are cases in which you can combine the values you want to filter into a single property. For example, please check my answer below:

If you're allowed to change the database schema, then you should consider denormalizing the data. So a structure like below will do the trick:

db
|
--- Queue
     |
     --- Accounting Office
           |
           --- served
           |    |
           |    --- A1
           |    |   |
           |    |   --- serving_token: "A001"
           |    |   |
           |    |   --- ticket_status: "served"
           |    |
           |    --- A2
           |        |
           |        --- serving_token: "A002"
           |        |
           |        --- ticket_status: "served"
           |
           --- not served
                |
                --- A3
                |   |
                |   --- serving_token: "A003"
                |   |
                |   --- ticket_status: "not served"
                |
                --- A4
                |   |
                |   --- serving_token: "A004"
                |   |
                |   --- ticket_status: "not served"
                |
                --- A5
                |   |
                |   --- serving_token: "A005"
                |   |
                |   --- ticket_status: "not served"
                |
                --- A6
                    |
                    --- serving_token: "A006"
                    |
                    --- ticket_status: "not served"

Now you can query the "not served" node, using a combination of Query#orderByKey() and Query#endAt(java.lang.String)

db.child("Queue").child("Accounting Office").child("not served").orderByKey().endAt("A6");
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

You can try this approach without query:

ArrayList servedArray ;
ArrayList notServedArray ;
  • get a data snapshot
  • loop through snapshot
  • if value == "served" >> add to servedArray
  • else if value equal == "not served" add to notServedArray

And for count you can use :

servedArray.size
notServedArray.size
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Djoxy
  • 54
  • 4
  • This implies that the OP downloads the entire node and performs the filtering on the client, right? Isn't it a bit costly? – Alex Mamo Feb 14 '23 at 13:53
  • From the Firebase Documentation, they say "Note: Filtering and sorting can be expensive, especially when done on the client... ", that's why i avoided the query option. I don't think so, if the max of the node size is 1000 tokens. – Djoxy Feb 14 '23 at 15:12
  • Yes, that's correct, *Filtering and sorting can be expensive, especially when done on the client*. And as far as I understand, this is what your answer recommends. – Alex Mamo Feb 14 '23 at 15:15
  • Thank you very much, i was wrong, i thought the term "expensive" is about the Firebase billing. – Djoxy Feb 14 '23 at 15:22