过滤基于嵌套子键的Firebase列表?

时间:2016-09-24 01:05:20

标签: firebase firebase-realtime-database

Firebase docs for Structuring Data中,他们将以下数据结构作为将用户映射到组的示例。

{
  "users": {
    "alovelace": {
      "name": "Ada Lovelace",
      // Index Ada's groups in her profile
      "groups": {
         // the value here doesn't matter, just that the key exists
         "techpioneers": true,
         "womentechmakers": true
      }
    },
    ...
  },
  "groups": {
    "techpioneers": {
      "name": "Historical Tech Pioneers",
      "members": {
        "alovelace": true,
        "ghopper": true,
        "eclarke": true
      }
    },
    ...
  }
}

使用该结构,我将如何仅查询具有特定成员的组?因此,只有alovelace所属的组才会成员。

我会遵守规则吗?如果是这样,该规则会是什么样子?

2 个答案:

答案 0 :(得分:2)

OrderByChild在这种情况下工作 - 在查询对象

下面
angularfire.database.list('.../groups', { 
    query: {
      orderByChild: 'members/alovelace'+,
      startAt: true
    } 
  });

不确定如何将性能与Frank van Puffelen的答案进行比较 - 可能会更糟糕,因为它是另一个列表查询,而不仅仅是一些直接对象查找。

答案 1 :(得分:1)

该信息已存在于数据模型中。在/users/alovelace/groups下面,您有一个她成为会员的小组列表。

推荐此模型的原因是它甚至不需要查询来加载组列表,只需要直接读取/users/alovelace/groups