Firebase查询双重嵌套 [英] Firebase Query Double Nested
问题描述
{用户:
1234567:{
name:'Bob',
blogs:{
'abc':{..},
'zyx':{..}
}
} ,
7654321:{
name:'Frank',
blogs:{
'efg':{..},
'hij':{。 。}
}
}
}
Firebase API只允许您将子级过滤为一级(或 orderByChild 和 因此,如果不修改/扩展当前的数据结构,只保留选项来检索所有数据并过滤客户端: 这当然是非常低效的,而且当您拥有不重复的用户数/博客。因此,常见的解决方案就是将所寻找的关键字映射到所在的路径,即所谓的树索引。 然后,您可以使用以下方式快速访问博客: 您可能还想重新考虑是否可以重新构建数据以更好地适应您的使用情况和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.
The Firebase API only allows you to filter children one level deep (or with a known path) with its So without modifying/expanding your current data structure that just leaves the option to retrieve all data and filter it client-side: 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: Then you can quickly access the blog using: 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屋! 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();
});
});
{Users :
"1234567": {
name: 'Bob',
blogs: {
'abc':{..},
'zyx':{..}
}
},
"7654321": {
name: 'Frank',
blogs: {
'efg':{..},
'hij':{..}
}
}
}
orderByChild
and equalTo
methods.var ref = firebase.database().ref('Users');
ref.once('value', function(snapshot) {
snapshot.forEach(function(userSnapshot) {
var blogs = userSnapshot.val().blogs;
var daBlog = blogs['efg'];
});
});
{Blogs:
"abc": "1234567",
"zyx": "1234567",
"efg": "7654321",
"hij": "7654321"
}
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();
});
});