Firebase string match at end of field [duplicate]

和自甴很熟 提交于 2021-01-29 10:35:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!