【发布时间】:2017-03-19 22:01:37
【问题描述】:
我的数据库结构如下(简化):
{
"articles": {
"-uniqueId1": {
"title": "Article 1",
"category": "news"
},
"-uniqueId2": {
"title": "Article 2",
"category": "other"
},
"-uniqueId3": {
"title": "Article 3",
"category": "news"
},
"-uniqueId4": {
"title": "Article 4",
"category": "news"
}
},
"articlesByCategory": {
"news": {
"-uniqueId1": true,
"-uniqueId3": true,
"-uniqueId4": true
},
"other": {
"-uniqueId2": true
}
}
}
查询需要获取特定文章类别不在其中的文章。那有意义吗?假设uniqueId2 属于“其他”类别,则查询将仅获取“新闻”和所有其他现有类别中的文章。但是由于该列表可能包含数百万篇文章,因此我必须尽可能具体,并且不要获取与此确切标准不匹配的文章。因此,理想的查询如下:
const ref = firebase.database().ref("articlesByCategory");
ref.orderByChild("-uniqueId2").equalTo(null).once("value", function(snapshot) {
console.log(snapshot.key);
});
在这里,我正在搜索不存在特定属性的类别(其中属性等于null)。这在这里解释:Firebase: Query to exclude data based on a condition
但是,执行这样的查询需要我在安全规则中的“/articlesByCategory”上为每篇文章的唯一 ID 编制索引。但是,在“.indexOn”数组中动态添加新的文章 ID 是不切实际且非最佳的,因为这最终会产生数百万个唯一(自动生成)的 ID:
{
"articles": {
".read": true,
".write": true
},
"articlesByCategory": {
".read": true,
".write": true,
".indexOn": ["-uniqueId1", "-uniqueId2", "-uniqueId3", "-uniqueId4"...] // List would be endless!
}
}
那么这是如何实现的呢?为什么我在 StackOverflow 上到处都将这类结构(倒排索引)视为理想的解决方案,但似乎没有人解决这个问题?
【问题讨论】:
标签: javascript node.js firebase firebase-realtime-database