0

If I have a database full of vehicles with information such as price, make, model, year, mileage, color, style, transmission, etc how do I structure my data so a user could filter through all of the data?

Note that I would prefer that the user can filter on a range of values, for example, on price where you want the price to be between $3000 to $6000. Is there a good way to do this? I assume you might be able to duplicate your data or hash the data somehow to be able to create a value you could search for but I'm not sure how to approach that kind of thing.

How much of this can be done server-side vs client-side? I'm at a loss.

For example, right now, you can only order by one key. Let's say my data structure is simply the list of cars and their keys and values.

cars {
    "someLongId": {
        price: 5000,
        make: "Honda",
        model: "Accord",
        year: 1996,
        mileage: 154000,
        color: "blue",
        transmission: "standard"
    },
}

The problem is that this structure only supports filtering on one key. I would need to duplicate this data in the following way in order to allow a user to receive all of the blue Hondas in the database:

carsByColor: {
    "blue": {
        "someLongId": {
            price: 5000,
            make: "Honda",
            model: "Accord",
            year: 1996,
            mileage: 154000,
            color: "blue",
            transmission: "standard"
        }
    }
}

They would then receive the blue Hondas like this:

var ref = firebase.database().ref('carsByColor/blue');
ref.orderByChild('make').equalTo('Honda').on('child_added', data => {
    // do something with data
});

What I want to know is, how would I structure my data if I wanted something like 8 different filters as stated in the beginning of the question. Is it a good idea to duplicate the data to a point where it may exist in 100+ places just so I can find all of the blue 1996 Honda Accord's that cost between $3000-$6000 and have a mileage lower than 180,000?

My understanding is that I would need to have different paths for EVERYTHING and all of my car data would be duplicated. So I would have a path called "carsByColor/blue/Honda/Accord/1996" but also for 1997, 1998, etc. However, I would also have to do this over for any other model of Honda as well. It seems like a lot of work just to get basic filtering. It's that or I do all of the searching on the server/client.

jeffmcnd
  • 592
  • 4
  • 14
  • This is a very broad question and impossible to answer directly. Do you have some code? Have you read [Sorting & Filtering Data](https://firebase.google.com/docs/database/ios/lists-of-data)? What's your structure look like? What platform are you using? There are also a number of questions here that cover similar topics. I would suggest first reviewing [How To Ask A Good Question](http://stackoverflow.com/help/how-to-ask) as well as [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Write some code and let us know when you get stuck! – Jay Mar 16 '17 at 21:49
  • @Jay I didn't think it was that broad. I'm asking how you structure data when you want to to filter on multiple keys. Firebase doesn't allow this through the API so you either have to denormalize your structure and/or do some server/client side filtering. I want to know how that is done, which is the question.I used the example of car database because cars have lots of different variables on which one might like to filter. Like Craigslist, you should be able to search for make, model, price, odometer, year, distance from you, etc. I've edited the question with some code and db structure. – jeffmcnd Mar 19 '17 at 18:38
  • Ah! You've edited the question and added good data! See my answer to [Complex Queries](http://stackoverflow.com/questions/42672790/firebase-for-complex-query-a-no-go/42701961#42701961) as well as [Query Multiple Where Clauses](http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase). The question has been asked before and there are a *lot* of options and solutions posted so may want do do a search on *[firebase]where query* – Jay Mar 20 '17 at 14:26
  • I think the real answer is that Firebase doesn't really support fine grained queries and makes the developer work extremely hard to get this kind of functionality through denormalization and duplication. That's my point of view. I love Firebase but I think it's probably not the best choice for this application. – jeffmcnd Mar 27 '17 at 18:41

0 Answers0