Shi*_*hel 0 firebase angularfire
我们如何从angularfire查询firebase以匹配两个条件.我想在以下两个条件下搜索AND匹配
或者什么是firebase中的验证,如.indexOn或.validate
json就像
{
"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"
}
}
Firebase查询目前只能包含一个orderBy...调用.它们还可以仅查询比返回的节点低一级的属性.
因此,您无法以最初希望的方式实现您的要求:
// THIS IS AN EXAMPLE THAT WILL NOT WORK
ref.child('color')
.orderByChild('color').equalTo('red')
.orderByChild('color/categories').equalTo('hex');
Run Code Online (Sandbox Code Playgroud)
在某些情况下,您可以引入一个顶级属性,该属性组合了您要过滤的值.因此,假设您的颜色只能在一个类别中.然后,您可以添加其他colorcategory属性:
{
"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"
}
}
并过滤:
ref.child('color')
.orderByChild('colorcategory').equalTo('redhex')
Run Code Online (Sandbox Code Playgroud)
当您有两个单值属性时,即使它们位于JSON结构中的不同级别,这将起作用,因为您正在将这些属性规范化并在正确级别上升级为单个值.
但由于您的类别是多值的,因此这种方法也行不通.我能想到的唯一方法是在数据上创建所谓的二级索引.
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"
Run Code Online (Sandbox Code Playgroud)
您必须在客户端应用程序中或通过运行创建它的服务器端进程,从您自己的代码创建和维护此索引.但是一旦你有了索引,就可以查询它:
ref.child('color_category_subcategory')
.orderByKey().equalTo('red_hex_333')
.on('value', function(snapshot) {...
Run Code Online (Sandbox Code Playgroud)
或按范围:
ref.child('color_category_subcategory')
.orderByKey().startAt('blue_hex').endAt('blue_hex~')
.on('value', function(snapshot) {...
Run Code Online (Sandbox Code Playgroud)
在~上面的是不是一个特殊的运算符,但只是发生在后的ASCII码序列的所有其它字符落在一个字符.所以过滤['blue_hex', 'blue_hex~']将给出所有蓝色六边形.
| 归档时间: |
|
| 查看次数: |
1000 次 |
| 最近记录: |