Firebase-获取包含密钥的所有记录 [英] Firebase - Get all records which contains a key

查看:54
本文介绍了Firebase-获取包含密钥的所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase中有一系列记录,如下所示:

I have a series of records in Firebase, like these:

{
  records: {
    n1: {
      f1: 1,
      f2: 0,
      f3: 1
    },
    n2: {
      f1: 0,
      f4: 1,
      f5: 0
    }
  }
}

我想使用索引来获取包含键f1的所有记录,无论其内容可能是(0还是1)

I want, using an index, to get al records that contains the key f1 whatever its content might be (0 or 1)

有没有办法用Firebase做到这一点?

There's a way to do this with Firebase?

推荐答案

Firebase查询无法测试属性的存在,只能测试其值.

Firebase Queries cannot test for the existence of a property, only for their value.

解决此限制的最简单方法是添加一个属性,该属性的值表示您正在寻找的属性的存在.是的...通过一个示例可能更容易理解:

The easiest way to work around this limitation is to add a property with a value that signals the existence of the property you're looking for. Yeah... that is probably easier to understand with an example:

{
    "records": {
        "n1": {
            "f1": 1,
            "f2": 0,
            "f3": 1,
            "has_f1": true
        },
        "n2": {
            "f1": 0,
            "f4": 1,
            "f5": 0,
            "has_f1": true
        },
        "n3": {
            "f7": 8,
            "f8": 0
        }
    }
}

与此有关,您可以查询:

With this you can query:

ref.child('records')
   .orderByChild('has_f1')
   .equalTo(true)
   .on('child_added', function(snapshot) {
       console.log(snapshot.val()); 
   })

这篇关于Firebase-获取包含密钥的所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆