Firebase-具有equalTo的深度查询orderByChild [英] Firebase - Deep Query orderByChild with equalTo

查看:113
本文介绍了Firebase-具有equalTo的深度查询orderByChild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图抓住其中的每个项目—尽管实际结构要大得多(因为很难重构数据库),但是我创建了一个示例数据结构.

I am trying to grab every project by the members within them -- I've created a sample datastructure though the actual structure is much larger (as in it would be difficult to restructure the database).

这是我的查询:

var ref = new Firebase(FBURL + '/chat/meta/project');

var email = 'kerry@email.com';

ref
    .orderByChild("email")
    .equalTo(email)
    .on("child_added", function(snapshot) {
        console.log(snapshot.val());
    }
);

重要的是要注意,如果删除.equalTo(email),它将返回所有项目",而当它仅返回其中的2个时.

It is important to note that if I remove the .equalTo(email) that it returns all of the "projects", when it should only return 2 of them.

以下是Firebase中的数据:

Here is the data in Firebase:

{
  "chat" : {
    "meta" : {
      "project" : {
        "-KAgjWOxjk80HIbNr68M" : {
          "name" : "Gman Branding",
          "url" : "http://localhost:3000/project/abc123fasd123cc/...",
          "date" : "2015-10-10T21:33:25.170Z",
          "member" : {
            "-KAgkD-2GVESwNwKP3fA" : {
              "email" : "abc@gman.com"
            },
            "-KAgkP3M4nug9Bjn-vY6" : {
              "email" : "def@gman.com"
            },
            "-KAgkP3OF0sUgc9x9p37" : {
              "email" : "ghi@gman.com"
            },
            "-KAgkaMyEOiXft6o-HbO" : {
              "email" : "kerry@email.com"
            }
          }
        },
        "-KAgl9xlPDU5T4072FgE" : {
          "-KAglqH9pxkhgC84_kAl" : {
            "name" : "YuDog",
            "url" : "http://localhost:3000/project/abc123fasd123cc/...",
            "date" : "2015-10-10T21:41:31.172Z"
          },
          "name" : "billing test 1",
          "url" : "http://localhost:3000/project/abc123fasd123cc/...",
          "date" : "2015-02-25T23:18:55.626Z",
          "dateNotifyUnread" : "2016-01-25T23:23:55.626Z",
          "member" : {
            "-KAglNsswyk66qUZNrTU" : {
              "email" : "kerry@email.com"
            }
          }
        },
        "-KAgltmLk2oOYhEDfwRL" : {
          "-KAgm1Jt5q53gzLm1GIh" : {
            "name" : "YuDog",
            "url" : "http://localhost:3000/project/abc123fasd123cc/...",
            "date" : "2015-10-10T21:41:31.172Z"
          },
          "name" : "YuDog",
          "url" : "http://localhost:3000/project/abc123fasd123cc/...",
          "date" : "2015-10-10T21:41:31.172Z",
          "member" : {
            "-KAgm1Jvss9AMZa1qDb7" : {
              "email" : "joe@yudog.com"
            }
          }
        },
        "-KAgluTcE_2dv00XDm1L" : {
          "-KAgm6ENmkpDiDG2lqZ4" : {
            "name" : "YuDog Landing Page",
            "url" : "http://localhost:3000/project/abc123fasd123cc/...",
            "date" : "2015-10-10T21:41:31.172Z"
          },
          "-KAgmBptbeInutRzNinm" : {
            "name" : "YuDog Landing Page",
            "url" : "http://localhost:3000/project/abc123fasd123cc/...",
            "date" : "2015-10-10T21:41:31.172Z"
          },
          "name" : "YuDog Landing Page",
          "url" : "http://localhost:3000/project/abc123fasd123cc/...",
          "date" : "2015-10-10T21:41:31.172Z",
          "member" : {
            "-KAgm6EQcvQg3oP-OnIF" : {
              "email" : "joe@yudog.com"
            },
            "-KAgmBpwoxPYGXS9fLZ9" : {
              "email" : "joe@yudog.com"
            }
          }
        }
      }
    }
  }
}

我在SO上查看了8-10个其他链接,但没有找到可以解决此问题的链接.

I've looked at 8-10 other links on SO but haven't found any that solve this issue.

推荐答案

解决方案是创建一个满足您需求的数据结构.在这种情况下,您要基于用户的电子邮件地址查找项目.因此,我们将添加一个包含此映射的节点:

The solution is to create a data structure that matches your needs. In this case, you want to look up the projects for a user based on their email address. So we'll add a node that contains this mapping:

"projects_by_email": {
  "kerry@email,com": {
    "-KAgjWOxjk80HIbNr68M": true,
    "-KAgl9xlPDU5T4072FgE": true
  },
  "abc@gman,com": {
    "-KAgjWOxjk80HIbNr68M": true
  }
  ...
}

这被称为对数据进行非规范化,尽管我经常将它们视为倒排索引.我可能会使用uid保留项目,但结构将是相同的.

This is called denormalizing your data, although I often think of them as inverted indexes. I would probably keep the projects by uid, but the structure would be the same.

使用这样的结构,您可以通过简单的直接查找来获取电子邮件的项目列表:

With a structure like this, you can get the list of projects for an email with a simple direct look up:

var ref = new Firebase(FBURL);
var email = 'kerry@email.com';
ref.child('projects_by_email')
   .child(email)
   .on("child_added", function(snapshot) {
       console.log(snapshot.key());
   }
);

或者,如果您随后还想加入"项目本身,则:

Or if you then also want to "join" the projects themselves:

var ref = new Firebase(FBURL);
var email = 'kerry@email.com';
ref.child('projects_by_email')
   .child(email)
   .on("child_added", function(snapshot) {
       ref.child('project').child(snapshot.key()).once('value', function(projectSnapshot) {
           console.log(projectSnapshot.val());
       });
   }
);

这种非规范化是NoSQL数据建模的常规部分.重复可能会造成浪费,但这是NoSQL解决方案如此之好扩展的部分原因:上面的代码都没有要求数据库考虑所有项目/所有用户.全部都是直接访问正确的节点,因此扩展性非常好.因此,我们正在牺牲存储空间来提高性能/可扩展性.典型的空间与时间的权衡.

This type of denormalizing is a normal part of NoSQL data modeling. The duplication may feel wasteful, but it is part of why NoSQL solution scale so well: none of the code above asks the database to consider all projects/all users. It's all directly accessing the correct nodes, which scales really well. So we're sacrificing storage space to gain improved performance/scalability; a typical space vs time trade-off.

这篇关于Firebase-具有equalTo的深度查询orderByChild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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