0

i'm trying to filter the data that i have in a database in firebase. I have a very simple structure of data like this.

{
  "articulos" : {
    "-LHYw-qz8fiTSg03TuYS" : {
      "descripcion" : "refgre",
      "fabricante" : "gerg",
      "iva" : "21%",
      "margen" : "23",
      "nombre" : "greerfg",
      "preciocompra" : "3213",
      "preciocompraiva" : "12312",
      "precioventa" : "323",
      "precioventaiva" : "23",
      "ref" : "4greg"
    },
    "-LHYwfIIoN46cjlF3VTR" : {
      "descripcion" : "gdfgdfg",
      "fabricante" : "dfgdf",
      "iva" : "21%",
      "margen" : "45",
      "nombre" : "gfdgdf",
      "preciocompra" : "4345",
      "preciocompraiva" : "543",
      "precioventa" : "543",
      "precioventaiva" : "45",
      "ref" : "43g34g"
    },
    "-LHYwvK6xAQeXHQVrmtt" : {
      "descripcion" : "fdsf",
      "fabricante" : "fdsf",
      "iva" : "21%",
      "margen" : "34",
      "nombre" : "dfsfsd",
      "preciocompra" : "sdfsd",
      "preciocompraiva" : "dfsd",
      "precioventa" : "43",
      "precioventaiva" : "34",
      "ref" : "432"
    }
  },
  "clientes" : {
    "-LHjGlhQD4iP9DOsGgvx" : {
      "ciudad" : "rtbrtbrt",
      "comentarios" : "gfdgfd",
      "correo" : "brt@hotmail.com",
      "cp" : "43243",
      "direccion" : "tbrb",
      "dni" : "rtbrtb",
      "id" : 2,
      "nombre" : "5brtbtrb"
    },
    "-LHjHImLC3UBW3M2Rf3_" : {
      "ciudad" : "fmdkjsf",
      "comentarios" : ".comsfdasa",
      "correo" : "dkfmsldfm@jnfdjsn",
      "cp" : "32434",
      "direccion" : "fkdmfkjsd",
      "dni" : "mklrmg",
      "id" : 2,
      "nombre" : "gkjsdnfgd"
    }
  }
}

i have been reading the documentation, and i have read about indexOn.

Currently i have this rules.

{  
 "rules": {
  ".read": true,
  ".write": true,

  "clientes": {
   ".indexOn": ["nombre"]
   }
  }
}

I'm not sure if i'm doing it well.. but when i send de URL, /clientes.json?orderBy="nombre"&startAt=x

it is retrieving all the "clientes" that i have in the DataBase...

How i supposed to do it for receive all the "clientes" that start at "x"... i'm trying to develop a search input in my website...

Sergio Cano
  • 690
  • 3
  • 13
  • 36
  • At first glance that query looks correct. Can you replace the picture of JSON with the actual JSON as text, so that I can give it a try? You can get this by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jul 23 '18 at 04:12
  • Hello! thank you for your answer, i replaced the img for JSON text! hope that you find the solution :P – Sergio Cano Jul 23 '18 at 04:23

1 Answers1

0

When I add your JSON to my database, it works as expected:

https://stackoverflow.firebaseio.com/51471121/clientes.json?orderBy="nombre"&startAt="x"

Gives me no results, because there is no name starting with x or later.

But:

https://stackoverflow.firebaseio.com/51471121/clientes.json?orderBy="nombre"&startAt="f"

Gives me one result, since "nombre" : "gkjsdnfgd" is after f.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • so...is it working? xD, so i really dont understand how it works. i tried the same but with "5"... and is retrieving both clients... and there is a "client" with "nombre": "5brtbtrb" // btw... sorry for those weird names, i was trying my form :P – Sergio Cano Jul 23 '18 at 04:45
  • i thought that if you send it... clientes.json?orderBy="nombre"&startAt="5" , you will receive only the client that has a name what contains "5"... – Sergio Cano Jul 23 '18 at 04:47
  • Firebase Database can only do so-called prefix filters, no contains filters. So the above, orders all client nodes, then finds the first one starting with `5` and returns all results from there. Also see https://stackoverflow.com/questions/22506531/how-to-perform-sql-like-operation-on-firebase – Frank van Puffelen Jul 23 '18 at 13:22
  • deleting firebase from my APP, what the hell!? i didnt expect it. it seems that i have to use MySQL again. Thanks a lot Frank! – Sergio Cano Jul 23 '18 at 13:36