0

My firebase users tree has this structure:

users:

{
 {
 'userName': 'abc',
 'userEmail' : '[email protected]',
 'userPreferences': 
   [ 
     0:'Cinema', 
     1:'It' 
   ] 
  },
  {
 'userName': 'abc',
 'userEmail' : '[email protected]',
 'userPreferences': 
   [ 
     0:'Cinema', 
     1:'Music' 
   ] 
 }
 } 

Then, I try to find all users that their preference list contain 'Cinema'.
I try this code:

var ref1 = new Firebase("https://event-application.firebaseio.com/users"); 
$scope.user = $firebaseArray(ref1.orderByChild("userpreferences").equalTo('Cinema'));
console.log($scope.user); 

But I don't get the best result. I get this record: enter image description here

1
  • 1
    What does "I don't get the best solution" means ? Commented Apr 28, 2016 at 10:59

2 Answers 2

1

Your JSON structure shows preferences as userPreferences, so wouldn't the following work?

var ref1 = new Firebase("https://event-application.firebaseio.com/users"); 
$scope.user = $firebaseArray(ref1.orderByChild("userPreferences").equalTo('Cinema'));
console.log($scope.user);

However I think there is also another problem with your code, you're called an .equalTo('Cinema') however you're comparing it to an array, correct me if i'm wrong but I don't think the behaviour of .equalTo('Cinema') is to loop through each of the values and compare them, I think it's just a straight up comparison

If this is the case, you may need to build a custom query by reading the data from firebase and manipulating it via function available to a snapshot

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. I edited my post. Yes that was my problem i don"t find how to compare .equalTo('Cinema') with array. And i must keep the same data structure
@SihemHcine Would you like me to go into reading data from firebase?
yes of course Sir. Could you give me your mail please
1

In NoSQL you'll often end up with a data model that reflects the way your application uses the data. If you want to read all the users that have a preference for Cinema, you should model that in your tree:

users: {
   'uid-of-abc': {
     'userName': 'abc',
     'userEmail' : '[email protected]',
     'userPreferences': [ 
        0:'Cinema', 
        1:'It' 
     ] 
   },
   'uid-of-def': {
     'userName': 'def',
     'userEmail' : '[email protected]',
     'userPreferences': [ 
       0:'Cinema', 
       1:'Music' 
     ] 
   }
},
"preferences-lookup": {
  "Cinema": {
     "uid-of-abc": true,
     "uid-of-def": true
  },
  "It": {
     "uid-of-abc": true
  },
  "Music": {
     "uid-of-def": true
  }
}

Now you can find out what users prefer cinema with:

ref.child('preferences-lookup/Cinema').on('value', function(snapshot) {
  snapshot.forEach(function(userKey) {
    console.log(userKey.key()+' prefers Cinema');
  });
});

This is covered in this blog post on denormalizing data with Firebase, in the Firebase documentation on structuring data and in dozens of answers here on Stack Overflow. A few:

1 Comment

Thank you Sir for your anwer :). But in my application structure(code) i have not to define preference for Cinema.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.