Jürgen Brandstetter
Jürgen Brandstetter

Reputation: 7324

Firebase query for existence of keys

I have the following data structure (of events) and want to query for all events a particular user (user1) is part of.

{
    "events":{
        "event1":{
            "users":{
                "user1":true,
                "user2":true
            }
        },
        "event2":{
            "users":{
                "user1":true
            }
        }
    }
}

The following query allows me to get all events user1 is part of but in a ways that does not allow me to create DB indexes.

starRef.child("events").orderByChild("users/user1").start(true).end(true)

I'm looking for a way that allows me to (at best) query for the existence of a key. For example starRef.child("events").orderByChild("users").hasKey("user1")

Upvotes: 0

Views: 83

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

To achieve this, i recomand you change your database structure a little bit, by adding a new node named events within each user. Your new node should look like this:

Firebase-root
   --- users
     --- user1
       --- events
         --- event1: true
         --- event2: true

To query for all events that corespond to a particular user, just add a listener to "Firebase-root/users/user1/events" and get all the events from which that user belongs.

Hope it helps.

Upvotes: 1

Related Questions