-1

I need to queryOrderedByValue() on a node whose key is auto-generated. My database is structured as per pic below (/users-pings/$uid/$uid/"auto-id").

What changes can I make to my query below to achieve this?

REF_USERS_PINGS.child(currentUid).child(forId).queryOrderedByValue().queryEqual(toValue: "true").observeSingleEvent(of: .value) { (snapshot) in


            if snapshot.exists(){

                handler(true)

            } else {

                handler(false)
            }

enter image description here

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Roggie
  • 1,157
  • 3
  • 16
  • 40

2 Answers2

2

Firebase Database queries run on each child node under the location where you run them, so they autoskip the third level under user-pings in your case.

You just need to specify what property to order/filter on, which seems to be active here. So something like:

REF_USERS_PINGS.child(currentUid).child(forId).queryOrdered(byChild: "active").queryEqual(toValue: "true").observeSingleEvent(of: .value) { (snapshot) in
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • aha! this makes complete sense and it worked! - thanks so much for your help :) – Roggie Jul 11 '18 at 07:32
  • a quick question: If i want to add an additional order/filter (eg; type), can I bind an additional `queryOrdered(byChild: "type").queryEqual(toValue: "compliment")` to the query? – Roggie Jul 11 '18 at 07:51
  • 1
    @Roggie No. Queries are for single children only. Uou cannot query where active = true *and* type = compliment. But you can store compound values and query on that. For example add another child node to your structure: type_active: "compliment_true". Note: potential issue in your structure; storing a bool value as a string "true" which is not equal to true so be careful if you query by that as if you are looking for a bool of true, you wont find it. Check out [compound value](https://stackoverflow.com/questions/45433558/how-to-apply-multiple-filter-in-firebase-query-in-swift/45469440#45469440) – Jay Jul 11 '18 at 21:58
  • As Jay said, Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. For an example of this and other approaches, see my answer here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Jul 12 '18 at 13:21
0

You need to use queryOrderedByKey() to return result related to that id only:

REF_USERS_PINGS.child(currentUid).child(forId).queryOrderedByKey().queryEqual(toValue: "-LH72MNIVX8o4KI3V9nL").observeSingleEvent(of: .value) { (snapshot) in

more info here:

https://firebase.google.com/docs/database/ios/lists-of-data#data_order

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • thanks for your suggestion, but this does not solve the problem, `LH72MNIVX8o4KI3V9nL` is auto-generated so this value will not be known in advance. – Roggie Jul 11 '18 at 07:33