我正在尝试通过包含其类别数组的 url 属性的 url 参数来过滤博客条目。
http://example.com/blog.html?filter="foo-bar"
我想列出其类别数组 url 值中包含过滤器参数的每个条目。
这是参赛作品。
- Title
- Content
- Categories
-[0]{Name: "foo bar", Url: "foo-bar"}
-[1]{Name: "Other", Url: "other"}
如何实现这一目标?
我已经尝试了一些 orderByChild('categories').equalTo(...)
但没有任何效果。
最佳答案
对于 Firebase 和其他 NoSQL 数据库结构,有一种称为非规范化的做法有助于排序和过滤。基本思想是拥有多种格式的相同数据,专为特定任务而设计。因此,就您而言,您可能会遇到这样的情况:
blogEntries: {
blogId1: {
Title: '...',
Content: '...',
Categories: [ 'categoryId1', 'categoryId2', ... ]
},
... more blog entries ...
}
blogCategories: {
categoryId1: {
Name: '...',
Url: '...'
},
... more categories ...
},
blogEntriesByCategories: {
'foo bar': [ blogId1, blogId5, ... ],
'other': [ blogId3 ],
...
}
然后,您可以在 blogEntriesByCategories
上进行搜索,以获取与 'foo bar'
匹配的所有博客 ID,然后将这些 ID 与下面的数据交叉引用与相应博客 ID 匹配的 blogEntries
。
借助 Firebase 3,您可以同时写入多个路径。因此,每当您添加、更新或删除博客条目时,您也会更新 blogEntriesByCategories
。这个blog entry有一篇关于如何使用 Firebase 3 完成此操作的精彩文章。
希望这有帮助!
关于javascript - Firebase 排序和过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39789500/