Firebase如何读取数值直到指定长度。

huangapple go评论73阅读模式
英文:

firebase how to read values until specified length

问题

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) {
    }
})
英文:

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) {
                                }
                            })

答案1

得分: 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).

英文:

> 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");

答案2

得分: 0

你可以尝试这种方法,不需要查询:

ArrayList servedArray;
ArrayList notServedArray;
  • 获取数据快照
  • 循环遍历快照
  • 如果值等于 "served",则添加到 servedArray
  • 否则,如果值等于 "not served",则添加到 notServedArray

对于计数,你可以使用:

servedArray.size
notServedArray.size
英文:

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

huangapple
  • 本文由 发表于 2023年2月13日 23:22:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定