moevans
moevans

Reputation: 319

Getting Firebase Parents based on Child Values

Say that my firebase data structure looks like this picture below:

enter image description here

I am trying to query the clients based on the customerNumber within settings.

My most recent attempt, I've tried the following:

var number = register.number;

var ref = new Firebase(fbUrl+'/clients/');

ref.orderByChild("settings").orderByChild("customerNumber").equalTo(number).on("child_added", function(snapshot) {
   console.log(snapshot.key());
});

but apparently you can't use two orderByChild() functions.

So how would I do this? I've built this project way too much to change my structure now so that is NOT an option or a solution that is possible at this point.

I want to return the entire parents based on the customerNumber matching within the settings of the parent data structure.

Upvotes: 1

Views: 1662

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

I'm not sure why the comment I left doesn't work for you. Here is the snippet I just used to reproduce it:

var ref = new Firebase('https://mine.firebaseio.com');
ref.child('clients')
   .orderByChild('settings/customerNumber')
   .equalTo('1234567890')
   .once('value', function(snapshot) { 
     console.log(snapshot.val()); 
   });

See this jsbin: http://jsbin.com/towewegapa/edit?js,console

Upvotes: 1

ilyass
ilyass

Reputation: 402

You should 'Prefer Flattened Data' as Firebase ask us to do in their documentation : structuring data in firebase

You could maintain a second index that map the custumerNumber with the Firebasekey in clients

Upvotes: 0

Related Questions