angularfire 有没有办法查询匹配的 AND 条件 [英] is there a way in angularfire to query for matching AND condition

查看:31
本文介绍了angularfire 有没有办法查询匹配的 AND 条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何从 angularfire 查询 firebase 以匹配两个条件.我想在两个条件下搜索 AND 匹配

  1. 如果您的密钥具有红色
  2. 如果您的密钥具有类别十六进制

或者像 .indexOn 或 .validate 这样的 firebase 验证是什么

json 就像

<前>{颜色" : {-UqPNlZ8ddgdGMFSsD":{红色",类别":{十六进制":{子类别":[#111",#333"]}},状态":好的"},-U9P4pBpYiId3ID64K":{颜色":蓝色",类别":{十六进制":{子类别":[#ddd",#eee"]}},状态":好的"},-U9UgOdffZdfdbSydF":{颜色":绿色",类别":{十六进制":{子类别":[#333",#555"]}},状态":好的"}}

解决方案

一个 Firebase 查询当前只能包含一个 orderBy... 调用.它们也只能查询比返回节点低一级的属性.

因此您无法按照您最初希望的方式实现您的需求:

//这是一个不起作用的例子ref.child('颜色').orderByChild('color').equalTo('red').orderByChild('颜色/类别').equalTo('hex');

方法 1:添加一个新的属性来组合您要过滤的属性

在某些情况下,您可以通过引入一个顶级属性来组合您要过滤的值.因此,假设您的颜色只能属于一类.然后,您可以添加额外的 colorcategory 属性:

<前>{颜色" : {-UqPNlZ8ddgdGMFSsD":{红色","colorcategory": "redhex",类别":{十六进制":{子类别":[#111",#333"]}},状态":好的"},-U9P4pBpYiId3ID64K":{颜色":蓝色","colorcategory": "bluehex",类别":{十六进制":{子类别":[#ddd",#eee"]}},状态":好的"},-U9UgOdffZdfdbSydF":{颜色":绿色","colorcategory" : "greenhex",类别":{十六进制":{子类别":[#333",#555"]}},状态":好的"}}

并对其进行过滤:

ref.child('color').orderByChild('colorcategory').equalTo('redhex')

当您有两个单值属性时,即使它们在 JSON 结构中处于不同级别,这也会起作用,因为您将这些属性和级别规范化为正确级别上的单个值.

但由于您的类别是多值的,因此这种方法也行不通.我能想到的唯一方法是在数据上创建所谓的二级索引.

方法 2:在要过滤的属性上创建二级索引

NoSQL 数据库中的索引是一种数据结构,很像关系数据库中的多列索引,只存在于您的查询中.在这种情况下,由于您想查询颜色和类别的组合,我希望索引 color_category 甚至 color_category_subcategory.

color_category_subcategory:"red_hex_111": "-UqPNlZ8ddgdGMFSsD""red_hex_333": "-UqPNlZ8ddgdGMFSsD""blue_hex_ddd": "-U9P4pBpYiId3ID64K""blue_hex_eee": "-U9P4pBpYiId3ID64K""green_hex_333": "-U9UgOdffZdfdbSydF""green_hex_555": "-U9UgOdffZdfdbSydF"

您必须通过自己的代码创建和维护此索引,无论是在客户端应用程序中还是通过运行创建它的服务器端进程.但是一旦你有了索引,你就可以查询它了:

ref.child('color_category_subcategory').orderByKey().equalTo('red_hex_333').on('value', function(snapshot) {...

或按范围:

ref.child('color_category_subcategory').orderByKey().startAt('blue_hex').endAt('blue_hex~').on('value', function(snapshot) {...

上面的 ~ 不是一个特殊的运算符,而只是一个碰巧落在 ASCII 序列中所有其他字符之后的字符.所以过滤 ['blue_hex', 'blue_hex~'] 将给出所有的蓝色十六进制.

How we can query to firebase from angularfire to match two conditions. i wanted to search below two conditions as AND match

  1. if your key has color red
  2. if your key has a categories hex

or what will be the validation in firebase like .indexOn or .validate

json is like

{

  "color" : {
    "-UqPNlZ8ddgdGMFSsD" : {
      "color" : "red",
      "categories" : {
        "hex" : {
          "subcategory" : [ "#111", "#333" ]
        }
      },
      "status" : "ok"
    },
    "-U9P4pBpYiId3ID64K" : {
      "color" : "blue",
      "categories" : {
        "hex" : {
          "subcategory" : [ "#ddd", "#eee" ]
        }
      },
      "status" : "ok"
    },
    "-U9UgOdffZdfdbSydF" : {
      "color" : "green",
      "categories" : {
        "hex" : {
          "subcategory" : [ "#333", "#555" ]
        }
      },
      "status" : "ok"
    }
}

解决方案

A Firebase query can currently only contain one orderBy... call. They can also only query properties that are one level below the nodes being returned.

So you cannot implement your requirements in the way you probably initially hoped:

// THIS IS AN EXAMPLE THAT WILL NOT WORK
ref.child('color')
   .orderByChild('color').equalTo('red')
   .orderByChild('color/categories').equalTo('hex');

Approach 1: add a new property that combines the properties you want to filter on

In some cases, you can get away with introducing a top-level property that combines the values you want to filter on. So let's say your color can only be in one category. You could then add an additional colorcategory property:

{

  "color" : {
    "-UqPNlZ8ddgdGMFSsD" : {
      "color" : "red",
      "colorcategory": "redhex",
      "categories" : {
        "hex" : {
          "subcategory" : [ "#111", "#333" ]
        }
      },
      "status" : "ok"
    },
    "-U9P4pBpYiId3ID64K" : {
      "color" : "blue",
      "colorcategory" : "bluehex",
      "categories" : {
        "hex" : {
          "subcategory" : [ "#ddd", "#eee" ]
        }
      },
      "status" : "ok"
    },
    "-U9UgOdffZdfdbSydF" : {
      "color" : "green",
      "colorcategory" : "greenhex",
      "categories" : {
        "hex" : {
          "subcategory" : [ "#333", "#555" ]
        }
      },
      "status" : "ok"
    }
}

And filter on that:

ref.child('color')
   .orderByChild('colorcategory').equalTo('redhex')

This will work when you have two single-value properties, even when they're on different levels in the JSON structure, since you're normalizing those properties and level into a single value on the right level.

But since your categories are multi-value, this approach also won't work. The only approach that I can think of is that you create a so-called secondary index on your data.

Approach 2: create a secondary index on the properties that you want to filter on

An index in a NoSQL database is a data structure, much like a multi-column index in a relational database, that just exists for your queries. In this case, since you want to query on the combination of color and category, I'd expect an index color_category or maybe even color_category_subcategory.

color_category_subcategory:
  "red_hex_111": "-UqPNlZ8ddgdGMFSsD"
  "red_hex_333": "-UqPNlZ8ddgdGMFSsD"
  "blue_hex_ddd": "-U9P4pBpYiId3ID64K"
  "blue_hex_eee": "-U9P4pBpYiId3ID64K"
  "green_hex_333": "-U9UgOdffZdfdbSydF"
  "green_hex_555": "-U9UgOdffZdfdbSydF"

You will have to create and maintain this index from your own code , either in the client-side application or by running a server-side process that creates it. But once you have the index in place, you can query it:

ref.child('color_category_subcategory')
   .orderByKey().equalTo('red_hex_333')
   .on('value', function(snapshot) {...

Or by range:

ref.child('color_category_subcategory')
   .orderByKey().startAt('blue_hex').endAt('blue_hex~')
   .on('value', function(snapshot) {...

The ~ above is not a special operator, but just a character that happens to fall after all other characters in an ASCII sequence. So filtering ['blue_hex', 'blue_hex~'] will give all blue hexes.

这篇关于angularfire 有没有办法查询匹配的 AND 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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