Firebase字符串在字段结尾处匹配 [英] Firebase string match at end of field
问题描述
想象以下数据:
{
"name": "Abcdef",
"age": 21
},
{
"name": "Rodrigo",
"age": 24
},
{
"name": "Matt",
"age": 30
},
{
"name": "Def",
"age": 21
}
如何使此搜索等效:
SELECT * FROM table WHERE name = "%def"
这样我就得到了这个结果:
so that I get this result:
{
"name": "Abcdef",
"age": 21
},
{
"name": "Def",
"age": 21
},
(或至少第一个结果,因为其%def)我已经尝试过 startAt
和 endAt
,并且也使用了"\ uf8ff"
,但似乎没有任何效果....
(or at least the first result because its %def)
I've tried startAt
and endAt
and also use "\uf8ff"
but nothing seems to work....
这应该是解决方案:
ref.orderByChild("name").endAt("def"+"\uf8ff").once('value'....
含义:它以'def'结尾,但是不起作用....
meaning: it ends with 'def' but this doesn't work....
(我正在使用Javascript SDK)
(I'm using the Javascript SDK)
(注意:我的问题不是大写字母D,它的字符串匹配)
(Note: my problem is not the capital D, its the string match)
推荐答案
您正在调用的方法称为 endAt()
,但是您尝试将其用作 endsWith()
.这是另一种操作,Firebase不支持.
The method you're calling is called endAt()
, but you are trying to use it as an endsWith()
. That is a different type of operation, and is not supported by Firebase.
Firebase数据库查询只能执行前缀匹配:查找以某个子字符串开头的字符串.没有用于后缀匹配或查找包含特定值的字符串的操作.
Firebase Database queries can only do prefix matching: find strings that start with a certain substring. There is no operation for postfix matching, nor for finding strings that contain a certain value.
如果您的用例确实是一个后缀匹配项,则通常的解决方法是添加一个包含反向字符串的属性,并对该属性执行 startAt().endAt()
.
If your use-case is really a postfix match, the common workaround would be to add a property that contains the reverse string and do a startAt().endAt()
on that.
所以:
{
"name": "Abcdef",
"name_reverse": "fedcbA",
"age": 21
},
{
"name": "Def",
"name_reverse": "feD",
"age": 21
},
然后:
ref.orderByChild("name_reverse").startAt("fed").endAt("fed\uf8ff")
这篇关于Firebase字符串在字段结尾处匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!