Firebase查询双重嵌套 [英] Firebase Query Double Nested

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

问题描述

给定firebase下面的数据结构,我想运行一个查询来检索博客efg。

  {用户:
1234567:{
name:'Bob',
blogs:{
'abc':{..},
'zyx':{..}
}
} ,
7654321:{
name:'Frank',
blogs:{
'efg':{..},
'hij':{。 。}
}
}
}


解决方案

Firebase API只允许您将子级过滤为一级(或 orderByChild 和 equalTo .htmlrel =nofollow noreferrer>)方法。

因此,如果不修改/扩展当前的数据结构,只保留选项来检索所有数据并过滤客户端:



>  var ref = firebase.database()。ref('Users'); 
ref.once('value',function(snapshot){
snapshot.forEach(function(userSnapshot){
var blogs = userSnapshot.val()。blogs;
var daBlog = blogs ['efg'];
});
});

这当然是非常低效的,而且当您拥有不重复的用户数/博客。因此,常见的解决方案就是将所寻找的关键字映射到所在的路径,即所谓的树索引。


$ b {博客:
abc:1234567,
zyx :1234567,
efg:7654321,
hij:7654321
}

然后,您可以使用以下方式快速访问博客:
$ b

var ref = firebase.database()。ref(); ('Blogs'/'efg')。一次('value',function(snapshot){
var user = snapshot.val();
ref.child('Blogs / ('value + function'(blogSnapshot){
var daBlog = blogSnapshot.val();
});
});

您可能还想重新考虑是否可以重新构建数据以更好地适应您的使用情况和Firebase限制。他们在构建数据方面有一些很好的文档,但对于NoSQL /分层数据库的新手来说,最重要的文档似乎是避免筑巢

Given the data structure below in firebase, i want to run a query to retrieve the blog 'efg'. I don't know the user id at this point.

{Users :
     "1234567": {
          name: 'Bob',
          blogs: {
               'abc':{..},
               'zyx':{..}
          }
     },
     "7654321": {
          name: 'Frank',
          blogs: {
               'efg':{..},
               'hij':{..}
          }
     }
}

解决方案

The Firebase API only allows you to filter children one level deep (or with a known path) with its orderByChild and equalTo methods.

So without modifying/expanding your current data structure that just leaves the option to retrieve all data and filter it client-side:

var ref = firebase.database().ref('Users');
ref.once('value', function(snapshot) {
    snapshot.forEach(function(userSnapshot) {
        var blogs = userSnapshot.val().blogs;
        var daBlog = blogs['efg'];
    });
});

This is of course highly inefficient and won't scale when you have a non-trivial number of users/blogs.

So the common solution to that is to a so-called index to your tree that maps the key that you are looking for to the path where it resides:

{Blogs:
     "abc": "1234567",
     "zyx": "1234567",
     "efg": "7654321",
     "hij": "7654321"
}

Then you can quickly access the blog using:

var ref = firebase.database().ref();
ref.child('Blogs/efg').once('value', function(snapshot) {
    var user = snapshot.val();
    ref.child('Blogs/'+user+'/blogs').on('value, function(blogSnapshot) {
        var daBlog = blogSnapshot.val();
    });
});

You might also want to reconsider if you can restructure your data to better fit your use-case and Firebase's limitations. They have some good documentation on structuring your data, but the most important one for people new to NoSQL/hierarchical databases seems to be "avoid building nests".

这篇关于Firebase查询双重嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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