[英]How to fetch all parent nodes with similar child nodes on Firebase database Android?
Screen shot of my Database 我的数据库的屏幕截图
I would like to query my database by returning the names of all parents nodes which have certain similar child nodes. 我想通过返回具有某些相似子节点的所有父节点的名称来查询数据库。
I am already fetching the name of one parent with respect to it's child, but I want to fetch the name of all parents with similar child value. 我已经在获取有关其孩子的一位父母的名字,但是我想获取所有具有相似孩子价值的父母的名字。
Suppose, I want to get the names of the Hotels which have the similar child node value of 2000
and have a Rating of 3-star
. 假设,我想获得子节点值为
2000
且评级为3-star
。
final List<String> hotelNames = new ArrayList<String>();
final Query userQuery = FirebaseDatabase.getInstance().getReference().child("F-5").orderByChild("HotelTypeTwo");
userQuery.equalTo(varX).addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot foodSnapshot: dataSnapshot.getChildren()) {
hotelNames.add(foodSnapshot.getKey());
for (int i=0;i < hotelNames.size();i++)
{
Log.i("Value of element "+i,hotelNames.get(i));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
}
);
So to get all hotels where HotelTypeTwo
is equal to Desi
and get according to your screenshot as results: Islamabad Hotel
and Islamabad Mariott Hotel
, please use the follwing lines of code: 因此,要获取所有
HotelTypeTwo
等于Desi
酒店,并根据您的屏幕截图作为结果获取: Islamabad Hotel
和Islamabad Mariott Hotel
,请使用以下代码行:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference fRef = rootRef.child("F-5");
Query query = fRef.orderByChild("HotelTypeTwo").equalTo("Desi");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String hotelName = ds.getKey();
Log.d(TAG, hotelName);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
query.addListenerForSingleValueEvent(valueEventListener);
The result in your logcat will be: 您的logcat中的结果将是:
Islamabad Hotel
Islamabad Mariott Hotel
Unfortunately, querying on multiple properties is not permitted in Firebase. 不幸的是,Firebase中不允许查询多个属性。 A workaround can be my answer from the following post:
一种解决方法可以是我从以下帖子中获得的答案:
However, in Firestore compound queries are permitted: 但是,在Firestore中,可以使用复合查询 :
citiesRef.whereEqualTo("state", "CO").whereEqualTo("name", "Denver");
citysRef.whereEqualTo(“ state”,“ CO”)。whereEqualTo(“ name”,“ Denver”);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.