Firebase - get elements by list of IDs

Viewed 4416

I'm using firebase as my database-backend and have a many to many relationship. As my relationship is a little bit more abstract I'll go with a simplified example here. Let's say we have students and lectures. Students can attend lectures and lectures have a list of attending students. Throughout the application, both queries are needed, so that there is no real advantage of keeping the list in either one of them. This issue seems not uncommon in No-SQL scenarios like this and I've even read about storing a list of all lectures for each student and a list of students for each lecture, being a valid solution. So lets say my JSON data looks like this:

"students": {
    "s1" : {
        "name": "A",
        "lectures": ["l1", "l2"]
    },
    "s2" : {
        "name": "B",
        "lectures": ["l2", "l3"]
    }

}

and

"lectures": {
    "l1" : {
        "title": "lecture1",
        "students": ["s1"]
    },
    "l2" : {
        "title": "lecture2",
        "students": ["s1", "s2"]
    },
    "l3" : {
        "title": "lecture3",
        "students": ["s2"]
    }
}

With s1, s2, l1, l2, l3 being the cryptic Firebase IDs. Using a setup like this, it is easy to write a query in order to get all lectures of student "s1" as an array ["l1", "l2"].

However, I can't figure out how to get the corresponding lecture elements using this set of IDs, as I can only query for one ID. I also want to avoid fetching all data and filtering it afterwards using Java-Code.

1 Answers
Related