1

I have this following structure:

{
  "users" : {
    "123" : {
      "activities" : {
        "horse" : "horse",
        "bike" : "bike"
      },
      "age" : 21
    },
    "124" : {
      "activities" : {
        "bike" : "bike"
      },
      "age" : 30
  }
}

I am trying to do something similar to:

SELECT * FROM users WHERE (activities = 'horse' OR activities = 'bike') AND age >= 21

Can I please get some pointers on this? If I didn't structured the data properly, can I also get some tips there?

edit: jsfiddle

Patrioticcow
  • 26,422
  • 75
  • 217
  • 337
  • Firebase queries can only order by/filter on a single property. In some cases you might be able to combine your conditions into a single property, but that doesn't seem possible here. See http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase and http://stackoverflow.com/questions/28034092/or-queries-in-firebase/28036032#28036032 – Frank van Puffelen Jun 04 '15 at 22:02
  • @FrankvanPuffelen I've looked over the links you sent, but for some reason, i cant use that code logic. though i understand the idea. Either the api changed meanwhile or something, but i can't get my result to filter properly – Patrioticcow Jun 04 '15 at 22:31
  • Feel free to share your code, preferably by *edit*ing it into your question. A jsfiddle would be even better. – Frank van Puffelen Jun 04 '15 at 22:34
  • @FrankvanPuffelen see my example. Right now it returns all. I've tried `.startAt('letsGame').endAt('letsGame')` and `equalTo('letsGame')` but no results – Patrioticcow Jun 04 '15 at 22:54
  • You can only query for properties one level deep. See http://stackoverflow.com/questions/29014556/angularfire-how-to-query-the-values-of-a-specific-key-in-an-array/29015595#29015595 – Frank van Puffelen Jun 04 '15 at 23:04

1 Answers1

2

I will mark this question as a duplicate, but this code might be helpful for you to get started building your own search index:

var ref = new Firebase('https://yours.firebaseio.com/users');
var index = new Firebase('https://yours.firebaseio.com/index');
function createIndex() {
    ref.once('value', function(snapshot) {
        snapshot.forEach(function(userSnapshot) {
            var user = userSnapshot.val();
            index.child(user.age).child(userSnapshot.key()).set(true);
            Object.keys(user.activities).forEach(function(activity) {
                index.child(activity).child(userSnapshot.key()).set(true);
            });
        });
    });
}

Since you want to search across all users, the code loops over all users (you should normally do this when the users are added or modified, so by listening for the child_ events). It then adds the relevant nodes to the index: one for the age and one for every category.

After running this on your data, the index node looks like this:

{
  "21": {"123":true},
  "30":{"124":true},
  "bike":{"124":true},
  "horse":{"123":true}
}

So with that you can get all users that are between 20 and 30 by:

ref.orderByKey().startAt("20").endAt("30").on('child_added'

Or all users with a "horse" activity by:

ref.orderByKey().equalTo("horse").on('child_added'
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807