0

I am trying to get filter Firebase using multiple fields. This is more or less my object in Firebase:

{
    "id": "-id",
    "category": "History",
    "level": "High School",
    "pointAmount": 128,
    "pointBoost": 0,
    "photoURL": "link"
},
{
    "id": "-id",
    "category": "Physics",
    "level": "Primary School",
    "pointAmount": 128,
    "pointBoost": 0,
    "photoURL": "link"
}

What I'm doing now, is using an array of checkboxes in React to grab the level and category to filter by. This part is done. My question is, how can I filter the elements coming in from the database? This is how I'm doing it right now:

componentDidMount() {
    const assignmentsRef = firebase
        .database()
        .ref('Works')
        .orderByChild('available')
        .equalTo(true)
        .limitToFirst(9);
    assignmentsRef.on('value', snapshot => {
        let assignments = snapshot.val();
        let newState = [];
        for (let assignment in assignments) {
            newState.push({
                id: assignment,
                category: assignments[assignment].category,
                level: assignments[assignment].level,
                pointAmount: assignments[assignment].pointAmount,
                pointBoost: assignments[assignment].pointBoost,
                photoURL: assignments[assignment].photoURL,
                workText: assignments[assignment].workText,
            });
        }
        this.setState({
            assignments: newState
        });
    });
}

So as you can see, I'm already doing orderByChild. Also there will be multiple variables which to filter by. For example: If I select History, and Physics I will get both objects. Same if I select History and Primary School, but if I select Physics I should only get the second object. How can I filter it? There will be over 10 filters.

Alex Ironside
  • 4,658
  • 11
  • 59
  • 119
  • Thanks for pointing that out @FrankvanPuffelen. I think I have misread the question. I will remove the duplicate comment and simply add it as a normal comment because it can still be usefull as a reference. – André Kool Jul 05 '18 at 13:42
  • 1
    Please take a look at [Query based on multiple where clauses in Firebase](https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase). It's not a duplicate (it's about WHERE queries instead of OR) but it still contains some useful information. – André Kool Jul 05 '18 at 13:45
  • Thanks André. Agreed on it being a useful reference. I'm always considering adding the OR case to my answer there too, but it's already quite dense. :-/ – Frank van Puffelen Jul 05 '18 at 14:27

1 Answers1

3

It looks like you're trying to do an OR of both conditions. There isn't any built-in support for returning items that match one of a number of conditions. You will have to fire a separate query for each condition, and then merge the results from all queries client-side. This is not as slow as you may expect, since Firebase will pipeline the queries over a single connection.

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