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

5
  • 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 stackoverflow.com/questions/26700924/… and stackoverflow.com/questions/28034092/or-queries-in-firebase/… Commented Jun 4, 2015 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 Commented Jun 4, 2015 at 22:31
  • Feel free to share your code, preferably by editing it into your question. A jsfiddle would be even better. Commented Jun 4, 2015 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 Commented Jun 4, 2015 at 22:54
  • You can only query for properties one level deep. See stackoverflow.com/questions/29014556/… Commented Jun 4, 2015 at 23:04

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.