根据子键检索用户

retrieve user based on child keys

我的数据结构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.

有什么办法可以实现吗?还是我必须以不同的方式构建数据?

您只需按特定子键 (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 } } }