简体   繁体   English

检查 firebase 实时数据库中许多子节点中的值

[英]Check for a value in many child nodes in firebase real-time database

I want to retrieve the text 'Private Group' from the child 'group' IF the child 'contacts' has the value '9aIMkiMa0bSuMLjUk3R5bLpnoQS2'.如果子“联系人”的值为“9aIMkiMa0bSuMLjUk3R5bLpnoQS2”,我想从子“组”中检索文本“私人组”。 I also want to check for the same value from the other child nodes of the parent child 'questions posts'.我还想从父子“问题帖子”的其他子节点检查相同的值。 I have struggled with it but in vain.我一直在努力,但徒劳无功。 This is the code that I have been using, but It has only produced errors.这是我一直在使用的代码,但它只产生了错误。

final DatabaseReference groupsRef = FirebaseDatabase.getInstance().getReference().child("questions posts");
    Query query = groupsRef.orderByChild("group").orderByChild("contacts");
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            arrayList.clear();
          if (snapshot.exists() && snapshot.hasChild(onlineUserId)){
              arrayList.add(groupsRef.getKey().toString());
              arrayAdapter.notifyDataSetChanged();
          }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

This is the image of the database structure这是数据库结构的图像在此处输入图片说明

A Firebase query operates on a flat list of nodes, and can only contain a single unknown key. Firebase 查询在节点的平面列表上运行,并且只能包含一个未知键。

From what I can tell you have at least two levels of unknown keys ( -MH...Ez12 , and 9alMk....QS2 ) and possibly three ( Private Group ).据我所知,您至少有两个级别的未知密钥( -MH...Ez129alMk....QS2 ),可能还有三个( Private Group )。 There is no way to query this structure with a database query, and you will need to come up with a different data structure to allow your use-case.无法使用数据库查询来查询此结构,您需要提出不同的数据结构以允许您的用例。

For example, if you want to allow finding the posts for a given contact ID, consider storing exactly that relationship:例如,如果您想允许查找给定联系人 ID 的帖子,请考虑准确存储该关系:

"contactsToPosts": {
  "$contactId": {
    "$postId": true
  }
}

For more on this sort data modeling trade-off, see:有关此类数据建模权衡的更多信息,请参阅:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM