javascript - Firebase 排序和过滤

标签 javascript firebase firebase-realtime-database

我正在尝试通过包含其类别数组的 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/

相关文章:

php - 从隐藏的 url 获取数据

javascript - Viewbag For Dropdown For - 一旦操作具有参数,就会删除选定的属性

javascript - 如何在 node.js 和 Cloud Functions for Firebase 上获取正确的日期和月份?

ios - 检查数据库值是否大于 Firebase 中的本地值

Firebase 登录失败

javascript - 为什么 TypeScript 不会为此 promise 链抛出编译错误?

javascript 操作 json 对象

android - 错误 : could not find com. google.gms :google-services:4. 2.0

javascript - Firebase 实时数据库 : Uncaught TypeError: firebase. 数据库不是函数

android - 在不保留信息的情况下保持 Firebase 同步状态