简体   繁体   English

如何在Firebase数据库Android上获取具有相似子节点的所有父节点?

[英]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 HotelIslamabad 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.

相关问题 如何在 Android Studio 中使用来自 Firebase 数据库的查询在不使用唯一 ID 的情况下获取子节点? - How to Fetch child nodes without using unique ids using Query from Firebase database in Android Studio? 如何在 android 工作室中的 firebase 数据库中读取子节点而不提及父节点? - How to read child nodes without mentioning parent node in firebase database within android studio? 访问 Android 的 Firebase 中的父节点和子节点 - Accessing parent and child nodes in Firebase for Android 如何从firebase数据库中获取所有子节点? - how to get all child nodes from firebase database? Firebase数据库从子android访问节点 - Firebase Database accessing nodes from child android 如何在java中获取JSON对象的所有节点和子节点? - How to fetch all the nodes and child nodes of JSON object in java? java - 如何从Java中的JSON对象获取从父节点到子节点的所有节点? - How to fetch all nodes from parent to child from JSON Object in java? 将子节点 Firebase 数据库 Android 检索到单独的变量中? - Retrieve child nodes Firebase Database Android into separate variables? 如何在zk中的动态树中选择父节点时选择所有子节点? - How to select all child nodes on selection of parent in a dynamic tree in zk? 如何删除父节点将其所有子节点? - How to delete a parent node will all its child nodes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM