0

Here is my structure of realtime database in firebase

{
    "student1" : {
        "name" : "somename",
        "skillset" : [
            "cpp",
            "c",
            "java"
        ],
        other properties
    },
    "student2" : {
        "name" : "somename",
        "skillset" : [
            "javascript",
            "c",
            "python"
        ],
        other properties
    },
    "student3" : {
        "name" : "somename",
        "skillset" : [
            "cpp",
            "java"
        ],
        other properties
    },
    "student4" : {
        "name" : "somename",
        "skillset" : [
            "java",
            "kotlin"
        ],
        other properties
    } }

I want to retrieve all the students having some specific set of all skills

e.g. skills = ["cpp","java"]
then answer should be ["student1","student3"]

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Onk_r
  • 836
  • 4
  • 21

2 Answers2

0

That is not possible under this structure, as firebase only support filtering by one child. In your case, you would need to get all the data and filter by code

Mocas
  • 1,403
  • 13
  • 20
0

Your current structure allows you to easily determine the skills for a user. It does however not make it easy to determine the users for a skill. To allow that, you'll need to add a reverse index, looking something like:

skills: {
  java: {
    student1: true,
    student3: true,
    student4: true
  },
  kotlin: {
    student4: true
  }
  ...
}

With the above you can look up the user IDs for a skill, and from there look up each user. For more on this, see my answer here: Firebase query if child of child contains a value

But this still won't allow you to query for users by multiple skills. To allow that, you'll have to add skill combinations to the new data structure. For example with the above skills, there is one user who knows both kotlin and Java:

skills: {
  java: {
    student1: true,
    student3: true,
    student4: true
  },
  java_kotlin: {
    student4: true
  }
  kotlin: {
    student4: true
  }
  ...
}

While this leads to extra data, it performs quite well in practice, since you can always directly access the data that you need (so there's no real database query needed).

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