根据子键

时间:2016-02-07 11:53:22

标签: json firebase firebase-realtime-database

我的数据结构https://<app name>.firebaseio.com/options为:

{
    'abc123': {},
    'efg456': {}
}

https://<app name>.firebaseio.com/users为:

{
    'test1': {
        options: {'abc123': true}
    },
    'test2': {
        options: {'abc123': true, 'efg456': true}
    }
}

我想根据他拥有的选项来检索用户。逻辑是:Find <user> where <some option> is found in <user>.<options>

我尝试过类似:users.orderByChild('options').startAt('abc123').endAt('abc123').on('value', (snapshot) => {});;但这给了我null

有没有办法实现这个目标?或者我是否必须以不同的方式构建数据?

1 个答案:

答案 0 :(得分:1)

您只需按特定子密钥(options/abc123)排序,并检查结果是否为true;

> users.on('value', (snapshot) => { console.log(snapshot.val()) })

{ test1: { options: { abc123: true } },
  test2: { options: { abc123: true, efg456: true } },
  test3: { options: { efg456: true } } }

> users.orderByChild('options/abc123')
       .equalTo(true)
       .on('value', (snapshot) => { console.log(snapshot.val())})

{ test1: { options: { abc123: true } },
  test2: { options: { abc123: true, efg456: true } } }

> users.orderByChild('options/efg456')
       .equalTo(true)
       .on('value', (snapshot) => { console.log(snapshot.val())})

{ test2: { options: { abc123: true, efg456: true } },
  test3: { options: { efg456: true } } }