按多个字段过滤Firebase

问题描述 投票:0回答:1

我正在尝试使用多个字段来过滤Firebase。这或多或少是我在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"
}

我现在正在做的是使用React中的一系列复选框来抓取levelcategory来过滤。这部分完成了。我的问题是,如何过滤来自数据库的元素?这就是我现在正在做的事情:

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
        });
    });
}

所以你可以看到,我已经在做orderByChild了。还有多个要过滤的变量。例如:如果我选择HistoryPhysics,我会得到两个对象。如果我选择HistoryPrimary School相同,但如果我选择Physics我应该只得到第二个对象。我该如何过滤它?将有超过10个过滤器。

javascript reactjs firebase firebase-realtime-database
1个回答
3
投票

看起来你正在尝试做两种情况的OR。对于返回符合多种条件之一的项目,没有任何内置支持。您必须为每个条件触发单独的查询,然后合并客户端所有查询的结果。从Firebase will pipeline the queries over a single connection开始,这并不像你想象的那么慢。

© www.soinside.com 2019 - 2024. All rights reserved.