0

I was in need of a database for filtering a huge list of elements I got (almost 6700 elements) using React Native. I was thinking about using a .JSON and send it over to Firebase Storage, but later on I found out that Firebase Realtime Database might be a better solution. However, I need to make queries with that list.

Here is a chunk of the .JSON list I got that I imported into Realtime Database:

[
  {
    "airport_name": "Goroka",
    "city": "Goroka",
    "country": "Papua New Guinea",
    "utc": "10.00",
    "continent": "U",
  },
  {
    "airport_name": "Madang",
    "city": "Madang",
    "country": "Papua New Guinea"
    "utc": "10.00",
    "continent": "U",
  },
...
//Repeat for more 6686 elements.
]

I need to make a query with Firebase Realtime Database where as the user type in a value in a search bar and the system should return elements that matches such value in the fields 'airport_name', 'city' and 'country' at the same time in a single query. Is that possible?

Also, I am having trouble trying to return a simple query like below:

  const airportDbReference = database().ref('/');

  async searchAirportByValue(value) {
    return await airportDbReference
      .orderByChild('country')
      .equalTo(value)
      .once('value')
      .then((snapshot) => {
        console.log(snapshot);
      });
  }

This always returning null for me. It's worth noting that I am using React Native Firebase for the Firebase connection. It's almost 6700 nodes inside the .JSON file, editing each individual note by hand would be out of question.

I appreciate the help! Thanks!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Leminur
  • 291
  • 2
  • 7
  • 22
  • 1
    Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. For an example of this and other approaches, see my answer here: https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Jun 24 '20 at 23:00
  • 1
    1) In your example you could have a property `"airport_name-city-country": "AMS-AMS-NL"` and then filter on just the airport name, airport name and city, or airport name, city and country. If you reverse the values in this property: `"country-city_airport_name": "NL-AMS-AMS"`, you can do a more useful query on say all airports in `NL`. 2) But even with this approach you may need multiple/many properties to cover all combinations. If that is the case for your use-case you may want to consider using another/additional database for such queries and using RTDB for its Realtime sync feature. – Frank van Puffelen Jun 24 '20 at 23:03

0 Answers0