1

I'm new with Firebase and I have a question about retrieving some data. Lets say I'm having the following data structure:

Screenshot of the JSON

The goal of my query is for a given country name I want to retrieve all the users that their destinations array contain this country. For example: for the query of "Jordan" it will retrieve: user2 and user3.

Anybody know how can I do such a query with Firbase Angular?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user4550050
  • 621
  • 1
  • 6
  • 21

1 Answers1

5

Firebase doesn't have a query operator that matches items in an array. In fact: Firebase recommends against using arrays for situations like the one you have. Instead, store the data like this:

advisors-countries: {
  user1: {
    destinations: {
      Angola: true,
      Australia: true
    }
  },
  user2: {
    destinations: {
      Angola: true,
      Austria: true,
      Jordan: true
    }
  },
  user3: {
    destinations: {
      Austria: true
      Egypt: true,
      Jordan: true
    }
  }
}

Now you can query users that have destination Jordan with:

var users = ref.child('advisors-countries').orderByChild('destinations/Jordan')
                                           .equalTo(true);

And then bind this to your AngularJS scope with:

$scope.users = $firebaseArray(users);

This type of data structure is called an index and you'll often find you need to add specific indexes to your NoSQL database to fit the querying requirements of your app. See this article on NoSQL data modeling for a good introduction on the topic.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks I changed it. But now if I want to do a more complicate query: For a given countries name I want to retrieve all the users and their prices which their destinations contains at least one of the query countries. For example: for the query of "Jordan" , "Angola" it will retrieve: user1 , user2 and user3. – user4550050 Jan 11 '16 at 23:03
  • 1
    Firebase Queries don't support that type of querying natively. So the same as before: you will have to model your data to allow the queries that you want to run. Read the article I linked, indexes are covered extensively. In this last case you're looking for #7 and/or #8. – Frank van Puffelen Jan 11 '16 at 23:14
  • You don't need to do some callback on retrieving the data? – user4550050 Jan 13 '16 at 09:27
  • 1
    Not when using AngularFire. All of that stuff is encapsulated in `$firebaseArray` and `$firebaseObject`. But please take some time to read the [AngularFire documentation](https://www.firebase.com/docs/web/libraries/angular/). A few hours there will answer most questions you have. – Frank van Puffelen Jan 13 '16 at 15:21
  • @FrankvanPuffelen can you please tell me the query for the case when I need to search for the users who went to say: Angola and Austria. In above case there is none but how to get a combined result? – AndroidBeginner Jan 20 '17 at 21:58
  • That would be filtering for two properties in the above data model, which is not supported. You'd have to come up with a data model that allows the filter, e.g. combining all countries in a single property `countries: "Angola_ Austria"`. But it will be even trickier for ranges. – Frank van Puffelen Jan 21 '17 at 02:35
  • Thanks. this totally changed my noSql queries life. – Nephelococcygia May 06 '17 at 19:37