[英]Filtering Firebase by multiple fields
I am trying to get filter Firebase using multiple fields. 我正在尝试使用多个字段获取过滤器Firebase。 This is more or less my object in 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"
}
What I'm doing now, is using an array of checkboxes in React to grab the level
and category
to filter by. 我现在正在做的是在React中使用一系列复选框来获取要过滤的level
和category
。 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
. 如您所见,我已经在执行orderByChild
。 Also there will be multiple variables which to filter by. 也将有多个变量可供过滤。 For example: If I select History
, and Physics
I will get both objects. 例如:如果我选择History
和Physics
我将同时获得两个对象。 Same if I select History
and Primary School
, but if I select Physics
I should only get the second object. 如果选择“ History
和Primary School
,则相同,但是如果选择“ Physics
,则应该仅获得第二个对象。 How can I filter it? 我该如何过滤? There will be over 10 filters. 将有10多个过滤器。
It looks like you're trying to do an OR
of both conditions. 看来您正在尝试对两个条件进行“ OR
。 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 . 这并不像您预期的那样慢,因为Firebase会通过单个连接对查询进行管道传输 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.