问题
Imagin the following data:
{
"name": "Abcdef",
"age": 21
},
{
"name": "Rodrigo",
"age": 24
},
{
"name": "Matt",
"age": 30
},
{
"name": "Def",
"age": 21
}
How do I make the equivlant to this search:
SELECT * FROM table WHERE name = "%def"
so that I get this result:
{
"name": "Abcdef",
"age": 21
},
{
"name": "Def",
"age": 21
},
(or at least the first result because its %def)
I've tried startAt
and endAt
and also use "\uf8ff"
but nothing seems to work....
This SHOULD be the solution:
ref.orderByChild("name").endAt("def"+"\uf8ff").once('value'....
meaning: it ends with 'def' but this doesn't work....
(I'm using the Javascript SDK)
(Note: my problem is not the capital D, its the string match)
回答1:
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 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.
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.
So:
{
"name": "Abcdef",
"name_reverse": "fedcbA",
"age": 21
},
{
"name": "Def",
"name_reverse": "feD",
"age": 21
},
And then:
ref.orderByChild("name_reverse").startAt("fed").endAt("fed\uf8ff")
来源:https://stackoverflow.com/questions/51053198/firebase-string-match-at-end-of-field