0

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)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Rodrigo Graça
  • 1,985
  • 3
  • 17
  • 24

1 Answers1

2

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")
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You are confirming that Firebase can't do this... This is walking backwards... "oh Firebase is great"... and then can't even do a regex or something else to find a string? – Rodrigo Graça Jun 27 '18 at 14:22
  • So stupid that we can do prefix matching but no postfix or "in the middle" ... what a shame... – Rodrigo Graça Jun 27 '18 at 14:30
  • 1
    The Firebase Database is focused on fast synchronization of data between clients. As a result of that, its query capabilities are relatively simple when compared to many other databases you may have used. The answer Ron linked shows one way to combine Firebase with a full-text search engine, if you have a use-case that requires that. By combining dedicated solutions in such a way you can build/compose elaborate solutions for almost any use-case. – Frank van Puffelen Jun 27 '18 at 16:16
  • This answer needs more love, very clear very clever work around. Thanks again @FrankvanPuffelen – Joe Lloyd Jul 12 '19 at 09:48