AngularFire - 如何查询denormalised数据? [英] AngularFire - How do I query denormalised data?

查看:177
本文介绍了AngularFire - 如何查询denormalised数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

玉任开始了新鲜与火力地堡。我读过这样的:<一href=\"https://www.firebase.com/docs/data-structure.html\">https://www.firebase.com/docs/data-structure.html我读过这样的:<一href=\"https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html\">https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html

Ok Im starting out fresh with Firebase. I've read this: https://www.firebase.com/docs/data-structure.html and I've read this: https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html

所以,我为一个似乎矛盾的其他适当困惑。你可以组织你的数据分层,但如果你希望它是可扩展的,然后没有。然而,这不是实际的问题。

So I'm suitably confused as one seems to contradict the other. You can structure your data hierarchically, but if you want it to be scalable then don't. However that's not the actual problem.

我有以下结构(请纠正我,如果这是错误的),一个博客引擎:

I have the following structure (please correct me if this is wrong) for a blog engine:

"authors" : {
  "-JHvwkE8jHuhevZYrj3O" : {
    "userUid" : "simplelogin:7",
    "email"   : "myemail@domain.com"
  }
},
"posts" : {
  "-JHvwkJ3ZOZAnTenIQFy" : {
    "state" : "draft",
    "body" : "This is my first post",
    "title" : "My first blog",
    "authorId" : "-JHvwkE8jHuhevZYrj3O"
  }
}

作者列表和职位名单。首先我想要得到的作者所在的 userUid 等于我的当前用户的 UID 。然后,我想要得到的职位,其中 AUTHORID 是提供给查询的人。

A list of authors and a list of posts. First of all I want to get the Author where the userUid equals my current user's uid. Then I want to get the posts where the authorId is the one provided to the query.

但我不知道如何做到这一点。任何帮助将是AP preciated!我使用AngularFire如果有差别。

But I have no idea how to do this. Any help would be appreciated! I'm using AngularFire if that makes a difference.

推荐答案

火力地堡是一个的NoSQL数据存储。这是一个JSON层次,没有传统意义上的SQL查询(这些不是用闪电般快速的实时OPS真正兼容;他们往往是缓慢且昂贵)。有一些地图缩小样式功能计划(合并视图和工具,以协助这一点),但您在present主要武器是正确的数据结构。

Firebase is a NoSQL data store. It's a JSON hierarchy and does not have SQL queries in the traditional sense (these aren't really compatible with lightning-fast real-time ops; they tend to be slow and expensive). There are plans for some map reduce style functionality (merged views and tools to assist with this) but your primary weapon at present is proper data structure.

首先,让我们来解决这个树状分层结构VS非规范化的数据。下面是你应该非规范化的几件事情:

First of all, let's tackle the tree hierarchy vs denormalized data. Here's a few things you should denormalize:


  • 列出了你希望能够快速迭代(用户名列表,而无需下载用户写过的每封邮件或所有有关用户的其他元信息)

  • 您浏览的部分,比如一个用户属于(你应该可以不用下载所有组/间在系统中获取的房间给定用户的列表房间/组的列表大型数据集,所以把指数一处,主人房的数据在其他地方)

  • 有超过1000条记录(保持瘦速度)任何

  • 一个路径下的孩子,包含1..1(即可能是无限的)记录(从聊天室的元数据,例如聊天信息,这样你可以在不抓抓取所有的邮件对聊天室的信息)

下面是几件事情可能没有什么意义,非规范化:

Here's a few things it may not make sense to denormalize:


  • 数据,你总是取TOTO卫浴,绝不重复(如果你总是使用 .child(...)。在('值',...)来取一些记录,你该记录显示的一切,从来指的是父列表,没有理由来优化iterability)

  • 列出超过百元左右的记录您始终作为一个整体(如组的列表,用户所属的可能总是与用户获取并会平均5-10项短;或许没有理由保持它分道扬镳)

  • data you always fetch en toto and never iterate (if you always use .child(...).on('value', ...) to fetch some record and you display everything in that record, never referring to the parent list, there's no reason to optimize for iterability)
  • lists shorter than a hundred or so records that you always as a whole (e.g. the list of groups a user belongs to might always be fetched with that user and would average 5-10 items; probably no reason to keep it split apart)

抓取笔者一样简单只需添加id来的网址:

Fetching the author is as simple as just adding the id to the URL:

var userId = 123;
new Firebase('https://INSTANCE.firebaseio.com/users/'+userId);

要获取属于某个用户的职位名单,无论是维护用户的信息的索引:

To fetch a list of posts belonging to a certain user, either maintain an index of that users' posts:

/posts/$post_id/...
/my_posts/$user_id/$post_id/true

var fb = new Firebase('https://INSTANCE.firebaseio.com');
fb.child('/my_posts/'+userId).on('child_added', function(indexSnap) {
   fb.child('posts/'+indexSnap.name()).once('value', function(dataSnap) {
       console.log('fetched post', indexSnap.name(), dataSnap.val());
   });
});

样的工具 Firebase.util 可与已拆分存储,直到正火的数据协助火力地堡的观点和高级查询utils的释放:

A tool like Firebase.util can assist with normalizing data that has been split for storage until Firebase's views and advanced querying utils are released:

/posts/$post_id/...
/my_posts/$user_id/$post_id/true

var fb = new Firebase('https://INSTANCE.firebaseio.com');
var ref = Firebase.util.intersection( fb.child('my_posts/'+userId), fb.child('posts') );
ref.on('child_added', function(snap) {
   console.log('fetched post', snap.name(), snap.val();
});

或者简单地存储用户ID的帖子(取决于你的使用情况如何将数据提取后):

Or simply store the posts by user id (depending on your use case for how that data is fetched later):

/posts/$user_id/$post_id/...

new Firebase('https://INSTANCE.firebaseio.com/posts/'+userId).on('child_added', function(snap) {
   console.log('fetched post', snap.name(), snap.val());
});

这篇关于AngularFire - 如何查询denormalised数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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