Firebase数据库按内部字段选择 [英] Firebase Database select by inner field

查看:82
本文介绍了Firebase数据库按内部字段选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建查询以选择Firebase数据库中的数据时,我遇到了一些困难. 我的数据库结构是这样的:

I have some difficulties building a query for selecting data in firebase database. My database structure is like this:

我需要让所有与ghopper联系的用户.在这里结果是丁香和香紫苏.但是我真的不知道如何在Java中执行此简单查询.

I need to get all users which have contact ghopper. Here the result is alovelace and eclarke. But I really have no idea how to do this simple query in Java.

谢谢您的帮助!

推荐答案

您将为此使用Firebase数据库查询:

You'd use Firebase Database queries for that:

DatabaseReference usersRef = FirebaseDatabase.getInstance().ref("users");
Query query = usersRef.orderByChild("contacts/ghopper").equalTo(true);
// My top posts by number of stars
query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
            System.out.println(snapshot.getKey());
        }
    }

但这要求您为每个用户定义一个索引,该索引不会扩展.这是因为您现在拥有的结构旨在轻松确定聊天室的用户,而您正尝试相反的做法.

But this requires that you define an index for each user, which doesn't scale. That is because the structure you have now is meant to allow to easily determine the users for a chat room, and you're trying the opposite.

要有效地允许为用户获取聊天室,您应该对数据进行结构设计,以使其保留每个聊天室的用户列表.这通常称为次级/反向/反向索引.

To efficiently allow getting the chat rooms for a user, you should structure your data so that it also keep a list of users for each chat room. This is often called a secondary/reversed/inverted index.

有关此示例的更多信息,请参见:

For more examples of this see:

  • Firebase query if child of child contains a value
  • my answer on modeling many-to-many relationship in AskFirebase

这篇关于Firebase数据库按内部字段选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆