【问题标题】:Is it possible to get an array of unique values in firebase?是否有可能在 firebase 中获得一组唯一值?
【发布时间】:2015-12-13 19:31:48
【问题描述】:

考虑存储在 Firebase 中的以下数据:

{
  "beers": {
    "-jwhkclclmecl": {
      "name": "Two Hearted Ale",
      "type": "IPA",
      "brewery": "Bells"
    },
    "-ckqjheh292od": {
      "name": "Dirty Blonde Ale",
      "type": "Pale Wheat",
      "brewery": "Atwater"
    },
    "-hcwiu3cp902d": {
      "name": "Diabolical",
      "type": "IPA",
      "brewery": "North Peak"
    }
  }
}

是否可以为 Firebase 构建一个查询以返回其中一个子节点的唯一值数组。例如返回["IPA", "Pale Wheat"]

【问题讨论】:

    标签: angularjs firebase firebase-realtime-database angularfire


    【解决方案1】:

    不,您不能在集合中查询每个项目的特定属性的唯一值。你可以过滤这些值,所以ref.child('beers').orderByChild('type').equalTo('IPA')。但这不是你要问的。 :-)

    通常,如果您希望在 Firebase(或大多数其他 NoSQL 数据库)中进行此类操作,您会将项目(或对项目的引用)保存在一个组下:

    {
      "beer_types": {
        "IPA": {
          "-jwhkclclmecl": {
            "name": "Two Hearted Ale",
            "brewery": "Bells"
          },
          "-hcwiu3cp902d": {
            "name": "Diabolical",
            "brewery": "North Peak"
          }
        },
        "Pale Wheat": {
          "-ckqjheh292od": {
            "name": "Dirty Blonde Ale",
            "brewery": "Atwater"
          },
        }
      }
    }
    

    当然,这只有在您只想将它​​们归为一类时才有效。如果你想要多个类别,你可以在多个类别下存储对每个项目的引用:

    {
      "beers": {
        "-jwhkclclmecl": {
          "name": "Two Hearted Ale",
          "type": "IPA",
          "brewery": "Bells"
        },
        "-ckqjheh292od": {
          "name": "Dirty Blonde Ale",
          "type": "Pale Wheat",
          "brewery": "Atwater"
        },
        "-hcwiu3cp902d": {
          "name": "Diabolical",
          "type": "IPA",
          "brewery": "North Peak"
        }
      }
      "beer_types": {
        "IPA": {
          "-jwhkclclmecl": true,
          "-hcwiu3cp902d": true
        },
        "Pale Wheat": {
          "-ckqjheh292od": true
        }
      },
      "breweries": {
        "Bells": {
          "-jwhkclclmecl": true
        },
        "Atwater": {
          "-ckqjheh292od": true
        }
        "North Peak": {
          "-hcwiu3cp902d": true
        }
      }
    }
    

    【讨论】:

    • 很酷,谢谢。这[主要]是我正在做的事情,但我想确保 firebase 没有提供更简单的方法。谢谢!
    • 2020年还是这样吗?或者有什么办法,因为我非常需要这个功能,因为我将数据上传到 firebase。
    • 是的,还是一样。如果您希望某些东西是唯一的,请将这些东西存储为节点下的 key 并使用事务来改变该节点。搜索 unique values on Firebase 将为您提供有关该主题的更多问题(和答案)。
    【解决方案2】:

    Firebase 没有对数组的原生支持。如果你存储一个数组,它实际上会被存储为一个以整数作为键名的“对象”。

    // we send this
    ['IPA', 'Pale Wheat']
    // Firebase stores this
    {0: 'IPA', 1: 'Pale Wheat'}
    

    【讨论】:

      【解决方案3】:

      正如弗兰克所说,不。非规范化您想要选择 DISTINCT 的分组。就我而言,我想为城市和州填充下拉列表(HTML 选择组),它们是我每次会议的键/值对。因此,我将在状态下拉列表中填充我的“状态”firebase 节点的结果。选择州后,我将使用该州城市的列表填充城市下拉列表。

      至于如何在 Firebase 中创建这个组,下面是我想出的。只需用“item”和“group”更改“city”和“state”即可满足您的需求。无需在写入时检查重复项,因为默认情况下不写入。此函数将创建您的组(注意:在与它们一起调用之前将字符串设置为 .toLowerCase())

      function setCity(city,state){
          if (city && state){
              var statesRef = ref.child('states');
              var stateRef = statesRef.child(state);
              var obj = {};
              obj[city] = true;           
              stateRef.update(obj);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-02
        • 2022-01-18
        • 2018-10-20
        • 1970-01-01
        • 2021-05-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多