Firebase等于动态嵌套子级 [英] Firebase equalto dynamic nested child
问题描述
/ archive:{
$ userId:{
$ archiveKey: {
foo:1
},
...
},
...
}
其中 $ userId
引用了用户标识
和 $ archiveKey
是动态的,由 .push()
创建。
是否可以查询档案
ref并获取所有 archiveObjects
其中 foo = 1
?或者我需要抓取整个表,手动挖掘 $ userId
并提取 archiveObjects
我正在寻找Firebase查询现在可以查询嵌套的路径,但路径不能是动态的。
所以如果你知道uid,你可以用下面的语句查询这个用户的档案:
ref.child (1).on(...
如果您不知道uid,那么您将不得不创建一个数据结构,以便查找:
archive_category_to_uids:{
foo:{
1:{
uid1:true,
uid2:true
}
}
}
更常见的方法是将档案分成自己的顶级列表,并且拥有用户和类别参考:
users:{
userId1:{
archiveKey1:true,
...
},
...
},
存档:{
archiveKey1:{
foo:1,
uid:uid1
},
...
},
archiveCategories:{
foo:{
1:{
archiveKey1:
archiveKey2:true
$ b code $ $ $现在你可以找到档案:
ref.child('archiveCategories / foo / 1')。 ('value',function(keys){
keys.forEach(function(key){
ref.child('archives')。child(key.key())。once('value' ,函数(快照){
console.log(snapshot.val());
});
};
});
这个过程被称为非规范化,在NoSQL数据库中很常见。您正在为您的应用程序需要使用数据建模数据。欲了解更多关于这个和其他常见模式,我建议阅读这篇上。
With a structure of
/archive: {
$userId: {
$archiveKey: {
foo: 1
},
...
},
...
}
Where $userId
references a user id
and $archiveKey
is dynamic, created by .push()
.
Is it possible to query the archive
ref and get all archiveObjects
where foo = 1
? Or do I need to fetch down the whole table, manually dig into $userId
and extract the archiveObjects
I'm looking for?
解决方案 Firebase queries can nowadays query nested paths, but the paths cannot be dynamic.
So if you know the uid, you can query that user's archives with:
ref.child(authData.uid).orderByChild('foo').equalTo(1).on(...
If you don't know the uid, then you'll have to create a data structure that allows you to do the lookup:
archive_category_to_uids: {
foo: {
1: {
uid1: true,
uid2: true
}
}
}
A more common way is to separate the archives into their own top-level list and have both users and categories refer to that:
users: {
userId1: {
archiveKey1: true,
...
},
...
},
archives: {
archiveKey1: {
foo: 1,
uid: uid1
},
...
},
archiveCategories: {
foo: {
1: {
archiveKey1: true,
archiveKey2: true
}
}
}
Now you can get find the archives with:
ref.child('archiveCategories/foo/1').once('value', function(keys) {
keys.forEach(function(key) {
ref.child('archives').child(key.key()).once('value', function(snapshot) {
console.log(snapshot.val());
});
};
});
This process is called denormalization and is quite common in NoSQL databases. You're modeling the data for how your application needs to consume it. For more on this and other common patterns, I recommend reading this article on NoSQL data modeling.
这篇关于Firebase等于动态嵌套子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!