0

I have the following schema

Users:
      User1: // SOME USER ID
         friends:
              0:
                name: R
              1:
                name: T
      User2: // SOME USER ID
         friends:
              0:
                 name: X
              1: 
                 name: Y

I am trying to search for friends name under Users under friends

This is My code:

Query databaseQuerySlang = Database.RootReference.Child("Users")
        .OrderByChild("name").StartAt(_search).EndAt(_search + "\uf8ff");

I cant get it to work it always return an empty result.

Any Idea how to make it works

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Thudner
  • 1,043
  • 1
  • 7
  • 14

1 Answers1

1

Queries on the Firebase Realtime Database search at a fixed path of each child directly under the path where you run the query. Since you query on Users, Firebase checks directly under User1 and User2 for a property called name. Since such a property doesn't exist, there are no results.

In other words: your current data structure makes it easy to find the names of friends of a specific user, but not to find the friends of a specific user's name. To allow a lookup in both directions, you will also need to store the information that user X is a friend of User2 in a separate data structure.

friends: {
  "User1": {
    "R": true,
    "T": true,
  },
  "User2": {
    "X": true,
    "Y": true,
  },
  "R": {
    "User1": true,
  },
  "T": {
    "User1": true,
  },
  "X": {
    "User2": true,
  },
  "Y": {
    "User2": true,
  },
}

With this new data structure you can look up the friends of any user, and then look up the profile information for that user in a separate call.

For more on this, also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807