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

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

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"

  1. dbref.child("Queue").child(a.value.toString()).orderByChild("ticket_status").equalTo("not served").addValueEventListener(object : ValueEventListener{
  2. override fun onDataChange(snap: DataSnapshot) {
  3. val count = snap.childrenCount
  4. dbref.child("Student_Queue_Reserve").child(idNumber.toString()).child("people_ahead").setValue((count-1).toString())
  5. }
  6. override fun onCancelled(error: DatabaseError) {
  7. }
  8. })
英文:

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"

  1. dbref.child("Queue").child(a.value.toString()).orderByChild("ticket_status").equalTo("not served").addValueEventListener(object : ValueEventListener{
  2. override fun onDataChange(snap: DataSnapshot) {
  3. val count = snap.childrenCount
  4. dbref.child("Student_Queue_Reserve").child(idNumber.toString()).child("people_ahead").setValue((count-1).toString())
  5. }
  6. override fun onCancelled(error: DatabaseError) {
  7. }
  8. })

答案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:

  1. db
  2. |
  3. --- Queue
  4. |
  5. --- Accounting Office
  6. |
  7. --- served
  8. | |
  9. | --- A1
  10. | | |
  11. | | --- serving_token: "A001"
  12. | | |
  13. | | --- ticket_status: "served"
  14. |
  15. --- A2
  16. |
  17. --- serving_token: "A002"
  18. |
  19. --- ticket_status: "served"
  20. |
  21. --- not served
  22. |
  23. --- A3
  24. | |
  25. | --- serving_token: "A003"
  26. | |
  27. | --- ticket_status: "not served"
  28. |
  29. --- A4
  30. | |
  31. | --- serving_token: "A004"
  32. | |
  33. | --- ticket_status: "not served"
  34. |
  35. --- A5
  36. | |
  37. | --- serving_token: "A005"
  38. | |
  39. | --- ticket_status: "not served"
  40. |
  41. --- A6
  42. |
  43. --- serving_token: "A006"
  44. |
  45. --- 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:

  1. db
  2. |
  3. --- Queue
  4. |
  5. --- Accounting Office
  6. |
  7. --- served
  8. | |
  9. | --- A1
  10. | | |
  11. | | --- serving_token: "A001"
  12. | | |
  13. | | --- ticket_status: "served"
  14. | |
  15. | --- A2
  16. | |
  17. | --- serving_token: "A002"
  18. | |
  19. | --- ticket_status: "served"
  20. |
  21. --- not served
  22. |
  23. --- A3
  24. | |
  25. | --- serving_token: "A003"
  26. | |
  27. | --- ticket_status: "not served"
  28. |
  29. --- A4
  30. | |
  31. | --- serving_token: "A004"
  32. | |
  33. | --- ticket_status: "not served"
  34. |
  35. --- A5
  36. | |
  37. | --- serving_token: "A005"
  38. | |
  39. | --- ticket_status: "not served"
  40. |
  41. --- A6
  42. |
  43. --- serving_token: "A006"
  44. |
  45. --- ticket_status: "not served"

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

  1. db.child("Queue").child("Accounting Office").child("not served").orderByKey().endAt("A6");

答案2

得分: 0

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

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

对于计数,你可以使用:

  1. servedArray.size
  2. notServedArray.size
英文:

You can try this approach without query:

  1. ArrayList servedArray ;
  2. 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 :

  1. servedArray.size
  2. 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:

确定