0

My firebase database is structured like:

Users:
    -uid_1
         -contacts
    -uid_2
         -contacts
             -uid1
                 <Other Info>
    -uid_3
         -contacts
             -uid1
                 <Other Info>

I was wondering if there was a way to search for all objects with the uid1 object in their contacts lists.

I've tried to this code:

    usersRef.queryOrdered(byChild: "uid1").observe(.value, with: { (snapshot) in
        print(snapshot.value)
    })

But it ends up printing all of users' children.

  • You could get somewhere by `usersRef.queryOrdered(byChild: "contacts/uid1")`. But you'll need to define an index for each user to make this work efficiently, which seems non-feasible. I'd consider this a variant of a categorization problem, and recommend my answer for that: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Nov 26 '17 at 23:27
  • I've tried using the code, but it still prints all the objects in the database. I was wondering, can I use queryEqual on uid1's value, but use a null value instead? – TakeMeHomeCountryRoads Nov 27 '17 at 00:55

1 Answers1

0

You are trying to order the query by uid1, but that’s not a child of a user, contacts is.

In that case I would either fetch all users and then filter in code or restructure the database to allow for what you want.

JP Aquino
  • 3,946
  • 1
  • 23
  • 25