有没有一种方法在angularfire查询匹配和条件 [英] is there a way in angularfire to query for matching AND condition

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

问题描述

我们如何从angularfire查询到firebase以匹配两个条件。
i希望在以下两个条件下搜索 AND 匹配


  1. 红色

  2. 如果您的钥匙有类别

或者在firebase中进行验证,例如.indexOn或.validate

就像

 $ b $ color b 
$ b $ color b












$ b $ 类别:{
hex:{
subcategory:[#111,#333]
}
},
status:ok
},
-U9P4pBpYiId3ID64K:{
color:blue,
categories:{
hex:{
subcategory:[#ddd,#eee]
}

status:ok
},
-U9UgOdffZdfdbSydF:{
color:green,
< b>类别:{
hex:{
subcategory:[#333,#555]
}
},
status:ok
}
} $ b $ Firebase查询目前只能包含一个 orderBy ...


解决方案

呼叫。它们也只能查询比返回的节点低一级的属性。



所以你不能以你最初希望的方式实现你的需求:

  //这是一个不能工作的例子
ref.child('color')
.orderByChild('color ').equalTo('red')
.orderByChild('color / categories')。equalTo('hex');



方法1:添加一个新属性,将您要过滤的属性组合在一起



在某些情况下,您可以通过引入一个顶级属性来结合您要过滤的值。所以我们假设你的颜色只能属于一个类别。您可以添加一个额外的 colorcategory 属性:

 
{

color:{
-UqPNlZ8ddgdGMFSsD:{
color:red,
colorcategory:redhex,
类别:{
hex:{
subcategory:[#111,#333]
} $ b b













$ color = :
$ b子类别:[#ddd:bluehex,
categories ,#eee]
}
},
status:ok
},
-U9UgOdffZdfdbSydF:{
color :green,
colorcategory:greenhex,
categories:{
hex
subcategory:[#333,#555]
}
},
status:ok
}
}

然后过滤:

$ p $ ref.child('color')
.orderByChild('colorcategory')。equalTo('redhex')

有两个单值属性,即使它们在JSON结构中处于不同的级别,因为您正在将这些属性和级别规范化为正确级别的单个值。



但是既然你的分类是多值的,这种方法也行不通。我能想到的唯一方法就是在数据上创建一个所谓的二级索引。


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

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){...
pre>

上面的不是一个特殊的操作符,而是一个恰好落在所有其他字符之后的字符所以过滤 ['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查询匹配和条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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