Firebase DatabaseReference按指定值过滤 [英] Firebase DatabaseReference filter by specified values
问题描述
让我们假设我具有以下firebase JSON结构:
Lets suppose I have this firebase JSON structure:
我需要获得所有归因于"from"等于"this"的问题.
And I need to get all the questions which have the atribute "from" equal to "this".
我知道我可以使用Volley发出StringRequest并从questions.json中获取所有值.然后,在客户端,我可以遍历响应并删除那些没有正确属性的响应,但这仅在我有几个问题的情况下有效.如果我有数百万个问题,则需要遍历数百万个问题.
I know that I can use Volley to make a StringRequest and GET all values from my questions.json. Then, in the client side I could iterate over the response and remove the ones which don't have the correct atribute, but this would only work if I have a few questions. If I have millions of questions I would need to iterate over those millions of questions.
有什么办法可以对请求添加过滤器,以仅获取具有所选属性的问题? (例如:从这里的==搜索searchquestions.json)
Is there any way I could put a filter on my request to get only the question with an attribute of my choice? (Ex: search questions.json where from == this)
推荐答案
如果在您的情况下可能有用,请尝试
Try this if may be useful in your scenario
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child("questions").orderByChild("from").equalTo("this");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot issue : dataSnapshot.getChildren()) {
// do with your result
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这篇关于Firebase DatabaseReference按指定值过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!