How to query filter in Firebase?

Asked

Viewed 5,475 times

5

I’m starting at Firebase and would like to know how to query with filter, example:

Select, only people who live in Porto Alegre.

Today, what I have is a comic book structure.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I have four tables, street, neighborhood, city and state. The first that I posted is the table is state, the other is street/ street.

  • 1

    This will depend completely on how the data is arranged. Put an example of how your data tree is.

  • Sorry Pablo, but I haven’t done the structure yet. I only have my SQL structure.

  • 1

    Ah, I get it. So... Firebase, as you may know, is Nosql, and has structured data in the form of JSON (more or less). Unlike an SQL database, in which we work on a more abstract level, in Firebase you decide exactly where each element is. You’ll have to organize the data in Firebase so it’s easier to make this filter, but it’s kind of in hand. A good exercise might be trying to create an offline JSON and read from it. How would you do? If you follow the good practice of having an abstraction (Repository pattern, for example), do an implementation with JSON

  • Going to Firebase later will just be a matter of a few more tweaks.

  • Yes Pablo, I am aware that Firebase is Nosql and is structured in the JSON way. You suggest I assemble the structure, right into it and try to make the queries?

  • You can go straight to it or a JSON on your computer (or even XML). The important thing is to take the information that is in your current scheme and arrange it hierarchically. Once done, it will be clear how to make the query filtering as you need.

  • Yeah, I tried to do it by hand, right there, but I could only create data at the root, like, name and value, several, one below the other and just, I couldn’t put the kids.

  • Click "+" to create a new child.

  • 1

    Yes, I know that, but I mean, to query as with sql @Pabloalmeida.

  • I think this page here will help you. I’m also coming from mysql and this concept of noSQL for me is still weird. Querying lists

  • @Pabloalmeida Because, noSQL is a bit scary for those who come from mysql. But there is this series of articles in Portuguese that can help a lot: https://medium.com/android-dev-moz/firebasesql-4ee3d26a3d15#. h6w3goky4

  • You can convert your structure to Firebase easily. What has to be clear is that there is no "Where" in Firebase, but a filter in the references. If you want, following your example, to consult all the people who live you have to take the reference and use an equalTo on Child. But by experience, join these tables in a single, even if the fields are repeated. It makes it easier later when it comes to queries. Include address, city, state, etc all in person table.

Show 7 more comments

2 answers

2

Firebase uses a JSON-Tree structure, in your case you could create a 'child' called people, listing all the people in your system. Each person would be a knot with children contenting their characteristics.

firebase
   -pessoas
       -uid1
           -nome:"USER 1"
           -UF:"RS"
       -uid2
           -nome:"USER 2"
           -UF:"RS"
       -uid3
           -nome:"USER 3"
           -UF:"SC"

In your search, you can use "equalTo()" and filter by the specific "child", in which case you could search all people with the child "UF" equalTo() 'RS'

https://firebase.google.com/docs/database/android/lists-of-data#filtering_data

-2

The Firebase NAY works as an SQL Database Manager System. One of the main differences is that Firebase focuses on distributing direct data rather than filtering (Where) and gather separate data (Join). This means that if you’re careful enough, learn how Firebase works and model your data by following Firebase logic, you don’t need a dedicated server to darlings simple because they are made directly on the client’s device.

For more information, watch David East’s playlist on The Firebase Database for SQL Developers, available in https://www.youtube.com/playlist?list=PLl-K7zZEsYLlP-k-RKFa7RyNPa9_wCH2s.

  • Yes! And for those who don’t understand much English, you can read this series of articles in Portuguese :) https://medium.com/android-dev-moz/firebasesql-4ee3d26a3d15#. h6w3goky4

  • I believe it does not help the answer, because it is possible to make a filter the way it was asked. By the comments of the question he knows that really is Nosql the Firebase, but he would like to know if there is a equivalence in the query and how to do. In this case link solves the issue.

  • Wouldn’t it be better to put an example of how to get the documents that have the value "Porto Alegre" in the city countryside? 'Cause this explanation didn’t help at all

Browser other questions tagged

You are not signed in. Login or sign up in order to post.