Firebase - How to write multiple orderByChild to retrieve data?

I am looking for data where two fields are equal to what I pass.

Here is an example of my code:

this.refApp .orderByChild('userUid') .startAt(uid).endAt(uid) .orderByChild('jobId') .startAt(jobId).endAt(jobId) .on('value', (snap) => { //This currently doesn't get returned. }); 

In the above example, I am not getting any compiler errors, and the code seems normal. However, I hardcoded the data so that it returns an object equal to uid and jobid.

I can make this work for one orderByChild, but when I do two as above, it seems to do nothing.

+9
source share
3 answers

You can use only one ordering method.

To request more, you need to rethink the data structure. Your current structure probably looks something like this:

 { "key": { "id_1": { "userUid": "user_1", "jobId": "job_1" }, "id_2": { "userUid": "user_1", "jobId": "job_2" }, "id_3": { "userUid": "user_2", "jobId": "job_3" } } } 

With this structure, you can index one child key.

Now consider this structure:

 { "key": { "user_1": { "id_1": { "jobId": "job_1", "userUid": "user_1" }, "id_2": { "jobId": "job_2", "userUid": "user_1" } } "user_2": { "id_3": { "jobId": "job_3", "userUid": "user_2" } } } } 

This structure explicitly creates an index on the uid . So now, if you want to get all tasks from the user, you can write this request:

 var ref = new Firebase('<my-firebase-app>'); var uid = 'user_1'; var userRef = ref.child('key').child(uid); var query = userRef.orderByChild('jobId'); query.on('value', (snap) => console.log(snap.val()); 
+7
source

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.

+6
source

Not supported.

Depending on the type of request that you need to run in essence, you will need to either save your tasks by the user, for example, as @ david-east, or add a property that combines two properties:

 { jobId: 'J123', userId: 'provider:123', jobIdUserIdIndex: 'J123/provider:12345', [...] } 

But usually there is no way to resolve all the requests that you need. You either need to filter objects on the client side, or duplicate data to create another representation of the data; you will have job data saved in /jobs/$jobId , /users/$userId/jobs/$jobId and possibly /pendingJobs/$jobId .

It’s easier for me to use the orderBy * method only to order entities and duplicate data to select subsets of them.

I want Firebase to support indexes that combine multiple properties, but now you need to create them yourself. However, the presence of different views allows you to apply different security rules to them: for example, Only one user should have access to /users/$userId/jobs/ , only the administrator should have access to /jobs/$jobId .

0
source

Source: https://habr.com/ru/post/1238728/


All Articles