Another way to make the orderByChild in Firebase is to create an orderKey.
Imagine that you have this in your database:
{ userId: { firstName: 'Snow', lastName: 'Jon', birthYear: 283 } }
And you want to make a request: userRef.orderByChild('firstName').equalTo('Jon').orderByChild('lastName').equalTo('Snow')
You need to create a key for your user, for example:
{ userId: { firstName: 'Snow', lastName: 'Jon', birthYear: 283, orderName: 'JonSnow' } }
Thus, you can make a request as follows: userRef.orderByChild('orderName').equalTo('JonSnow')
This also works with startAt and endAt if you want to query all snow born over a number of years. First you create a key:
{ userId: { firstName: 'Snow', lastName: 'Jon', birthYear: 283, orderNameYear: 'Snow283' } }
So you can query: userRef.orderByChild('orderNameYear').startAt('Snow280').endAt('Snow285')
This will return all snow born between 280 and 285.
source share